I have the following code:
<a href="javascript:setCheckboxes3(1);" class="chkmenu">Check All</a> |
<a href="javascript:setCheckboxes3(0);" class="chkmenu">Uncheck All</a> |
<a href="javascript:setCheckboxes3(2);" class="chkmenu">Invert Selection</a><br />
<table>
<tr>
<td><input type="checkbox" name="names[8]" value="yes" />Paul</td>
<td><input type="checkbox" name="names[11]" value="yes" />Bob</td>
<td><input type="checkbox" name="names[44]" value="yes" />Tom</td>
</tr>
</table>
And the following script:
function setCheckboxes3(act)
{
elts = document.getElementsByName("names[]");
var elts_cnt = (typeof(elts.length) != 'undefined') ? elts.length : 0;
if (elts_cnt)
{
for (var i = 0; i < elts_cnt; i++)
{
elts[i].checked = (act == 1 || act == 0) ? act : (elts[i].checked ? 0 : 1);
}
}
}
The script is working with other arrays without keys, but I can't get it to work with this array which has keys.
Thanks in advance
Clicking on the master checkbox selects all checkboxes; and unchecking it, deselects all checkboxes.
1. when a user click on a checkbox if its contain noneoption=true then all other checkboxes are unchecked. 2. When a user click on checkbox that contain noneoption=false if a checkbox with noneoption=true checked it must unchecked.
You can use getElementsByClassName
:
<script type="text/javascript" language="javascript">
function setCheckboxes3(act) {
var e = document.getElementsByClassName('names');
var elts_cnt = (typeof(e.length) != 'undefined') ? e.length : 0;
if (!elts_cnt) {
return;
}
for (var i = 0; i < elts_cnt; i++) {
e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
}
}
</script>
<a href="javascript:setCheckboxes3(1);" class="chkmenu">Check All</a> |
<a href="javascript:setCheckboxes3(0);" class="chkmenu">Uncheck All</a> |
<a href="javascript:setCheckboxes3(2);" class="chkmenu">Invert Selection</a><br />
<input type="checkbox" name="names[8]" class="names" value="yes" />Paul
<input type="checkbox" name="names[11]" class="names" value="yes" />Bob
<input type="checkbox" name="names[44]" class="names" value="yes" />Tom
OR you can use: getElementsByTagName
<script type="text/javascript" language="javascript">
function setCheckboxes3(act) {
var e = document.getElementsByTagName('input');
var elts_cnt = (typeof(e.length) != 'undefined') ? e.length : 0;
if (!elts_cnt) {
return;
}
for (var i = 0; i < elts_cnt; i++) {
if((e[i].type) == 'checkbox') {
e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
}
}
}
</script>
<a href="javascript:setCheckboxes3(1);" class="chkmenu">Check All</a> |
<a href="javascript:setCheckboxes3(0);" class="chkmenu">Uncheck All</a> |
<a href="javascript:setCheckboxes3(2);" class="chkmenu">Invert Selection</a><br />
<input type="checkbox" name="names[8]" value="yes" />Paul
<input type="checkbox" name="names[11]" value="yes" />Bob
<input type="checkbox" name="names[44]" value="yes" />Tom
Did you get a chance to try jQuery?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With