Consider this simple code example:
<form name="text" id="frm1" method="post"> <input type="checkbox" name="name[]" value="1000"> Chk1<br> <input type="checkbox" name="name[]" value="1001"> Chk2<br> <input type="checkbox" name="name[]" value="1002"> Chk3<br> <input type="checkbox" name="name[]" value="1003"> Chk4<br> <input type="checkbox" id="select_all"/> Select All<br> </form> <form name="text" id="frm2" method="post"> <input type="checkbox" name="name[]" value="4000"> Chk1<br> <input type="checkbox" name="name[]" value="4001"> Chk2<br> <input type="checkbox" name="name[]" value="4002"> Chk3<br> <input type="checkbox" name="name[]" value="4003"> Chk4<br> <input type="checkbox" id="select_all"/> Select All<br>
I am trying to get Select All to work in every form (forms are dynamically generated in my production code and have different, changing names)
I use this jquery, but select_all only works for the first form; it does not affect forms below the first.
$('#select_all').change(function() { var checkboxes = $(this).closest('form').find(':checkbox'); if($(this).is(':checked')) { checkboxes.attr('checked', 'checked'); } else { checkboxes.removeAttr('checked'); } });
I canโt understand how to check the โCheck allโ checkboxes in any checkbox: the checkbox contained in the form identifier.
Can someone point me in the right direction?
Many thanks
jquery checkbox jquery-selectors forms
Chris
source share