Failed to trigger jQuery event for mobile clicks

I have a problem with the click function on a mobile web page. Here is my html code

<div data-role=content> <input type="text" id="text"> <div id="ss"></div> </div> <script type="javascript"> $(document).ready(function() { $("#text").keyup(function(){ $('#ss').append('<div style="background:yellow;" >Text<br/><a class="te"> alert </a></div>'); }); $(".te").click(function(){ alert("It is working"); }); }); </script> 

Please help me solve this problem.

+6
source share
2 answers

Your item is being added dynamically, use event delegation. Modify the click event to:

 $(document).on('click', '.te', function() { //do stuff }); 
+10
source

press the touch button OR 'touchstart'

 $('.delete-my-account').on('click touch', function(e) { e.preventDefault(); e.stopPropagation(); if (! confirm('Are you sure?')) { return false; } //... }); 
0
source

Source: https://habr.com/ru/post/946020/


All Articles