Skip to content Skip to sidebar Skip to footer

Dynamically Adding Click Event To Checkbox, Only Works For First Checkbox Jquery

So I am working with a couple ascx with different checkbox areas on each control. I originally used $('[type=checkbox]').click ( function(){ code }); and it did the job, At firs

Solution 1:

ids must be unique. Use a class instead:

<inputtype="checkbox"class="chck" />
$('.chck').click(function(){ code });

Solution 2:

See using same ids for multiple elems on a same page is invalid.

IDs should be unique to each element. When this happens browser only selects first of it.

To overcome this issue one must change id to class. So you have to do same thing with your markup change id to class:

<inputtype="checkbox"class="chck" /> // <----change id to classfor every input

and now your script should be:

$('.chck').each(function(){ //<-----use '.' notation for class selectors 
     $(this).click(function() { 
         onchange($(this));
     }); 
});

Post a Comment for "Dynamically Adding Click Event To Checkbox, Only Works For First Checkbox Jquery"