7

I have a problem with click function in mobile webpage. 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 with solving this problem.

chridam
  • 100,957
  • 23
  • 236
  • 235
Guwanch
  • 375
  • 3
  • 6
  • 19
  • Are you using jQuery Mobile? If so, please add [tag:jquery-mobile] to your question. – Mooseman May 28 '13 at 14:35
  • 1
    @Barlas, See this answer http://stackoverflow.com/questions/4755505/how-to-recognize-touch-events-using-jquery-in-safari-for-ipad-is-it-possible – maximkou May 28 '13 at 14:38

2 Answers2

16

Your element is added dynamically, use event delegation. Change your click event to:

$(document).on('click', '.te', function() {
    //do stuff
});
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • You, Sir, are a legend. I've been tearing my hair out over this for the last 3 hours. Works perfectly now that I've switched to this. THANK YOU!! – Steve Woods Sep 04 '15 at 13:21
0

'click touch' OR 'touchstart'

$('.delete-my-account').on('click touch', function(e) {
        e.preventDefault();
        e.stopPropagation();

        if (! confirm('Are you sure?')) {
            return false;
        }
        //...
});
Limitless isa
  • 3,689
  • 36
  • 28