JQuery- function activated once time -


i write in jquery function, select or unselect checkbox on page. have group checkbox.

  <input type="checkbox" title="111" value='111' name="cid[]" id='cb1'/>   ...   <input type="checkbox" title="nnn" value='nnn' name="cid[]" id='cbn'/> 

and have main check box.

<input type="checkbox" id="checkall" title="choose all" value="" name="checkall-toggle"/> 

when click on checkbox, checkbox must selected. use function:

$(":checkbox").click(function(){     var id = $(this).attr('id');     if (id=='checkall')     {         if (this.checked==true)         {              $('input[type=\'checkbox\']').attr('checked', true);          }else         {             $('input[type=\'checkbox\']').attr('checked', false);         }     } }); 

i use firefox 22.0 ... when click 'checkall' checkbox, function selected checkbox. after this, click second time on 'checkall' checkbox. result- checkbox unselected. but. when tried in second time select check box, not happend. why?

use .prop() instead of .attr()

$(":checkbox").click(function () {     var id = $(this).attr('id');     if (id == 'checkall') {         if (this.checked == true) {             $('input[type=\'checkbox\']').prop('checked', true);         } else {             $('input[type=\'checkbox\']').prop('checked', false);         }     } }); 

fiddle


Comments