Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check/Uncheck all checkboxes with Javascript in a PHP array with keys

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

like image 641
David Adders Avatar asked Oct 08 '11 19:10

David Adders


People also ask

How do I unselect all checkboxes?

Clicking on the master checkbox selects all checkboxes; and unchecking it, deselects all checkboxes.

How do I uncheck a checkbox by condition?

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.


1 Answers

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?

like image 175
Book Of Zeus Avatar answered Sep 22 '22 10:09

Book Of Zeus