1

I have got ajax result with new element on page.

Checkbox is:

<input type="checkbox" checked="checked" uid="933518636168122368" class="flag">

How I can select him?

my code is not working, selector object is empty:

$(document).ready(function(){
    if ($('.flag').is(':checked')) {
        alert('checkit');
    }

    $('.flag').click(function(){
        alert('checkit');
    });
});

What I must to do?

1 Answers1

1

Since the .flag element are created dynamically you need to use event delegation to register event handlers to these elements.

Try this

$(document).ready(function(){
    if ($('.flag').is(':checked')) {
        alert('checkit');
    }
    $(document).on('click','.flag',function(){
        alert('checkit');
    });
});

DEMO

Sridhar R
  • 20,190
  • 6
  • 38
  • 35