Jquery all checkbox select only works once -
i looked @ other threads didn't find solution work me.
i'm trying select checkboxes here html:
<input type="checkbox" id="selectall" onchange="toggleallworkers(this.checked);" />all<br> <input type="checkbox" class="emp" name="w0" value="1" onchange="buildpage();" />andrey <br> <input type="checkbox" class="emp" name="w1" value="4" onchange="buildpage();" />brian <br> <input type="checkbox" class="emp" name="w2" value="5" onchange="buildpage();" />eric <br> <input type="checkbox" class="emp" name="w3" value="3" onchange="buildpage();" />ken <br> <input type="checkbox" class="emp" name="w4" value="2" onchange="buildpage();" />kerry <br>
javascript
function toggleallworkers(status) { $("#clickmenu .emp").each( function(index) { console.log( index + ": "+$(this).attr("checked")+' status:'+status+' value:'+$(this).val()); $(this).attr("checked",status); console.log( index + ": "+$(this).attr("checked")); }); }
but works twice after page reload.
you should use prop
function. http://api.jquery.com/prop/
function toggleallworkers(status) { $('.emp').prop('checked',status); }
as well, there no need use each
, can done 1 simple selector.
Comments
Post a Comment