Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery dynamically added checkbox not working with change() function

I dynamically load in a few li's that have a label and a checkbox in them to another visible ul. I set the checkboxes to be checked="checked" and i am trying to trigger an event to happen when i change these dynamically inserted checkboxes but nothing occurs.

Here is the jquery:

$(".otherProductCheckbox:checkbox").change( function(){
    alert('test');
});

Here is the html for the dynamically added li's:

<li class="otherProduct"><input type="checkbox" class="otherProductCheckbox radioCheck" checked="checked"/><label>Product Name</label></li>

Any idea why i cant get the alert to happen when the checkbox changes its checked state?

like image 1000
estern Avatar asked Jan 14 '11 14:01

estern


2 Answers

You have to use liveQuery or live to bind events to dynamically added elements.

$(".otherProductCheckbox:checkbox").live('change', function(){
    alert('test');
});

EDIT To work in IE, you have to use click not change

$(".otherProductCheckbox:checkbox").live('click', function(){
        alert('test');
 });
like image 62
Teja Kantamneni Avatar answered Oct 24 '22 18:10

Teja Kantamneni


Since live() is now deprecated as of jQuery 1.7, I thought I'd post the solution that worked for me on the same problem (trigger event when dynamically added checkboxes are changed).

$(document).on('click', '.otherProductCheckbox', function() {
    alert('test');
});
like image 39
mvemjsunp Avatar answered Oct 24 '22 17:10

mvemjsunp