7

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

4
  • I'm confused...where's the PHP come in? Commented Oct 8, 2011 at 19:55
  • names[] is a PHP associative array. Commented Oct 8, 2011 at 19:57
  • no, it's not. Its HTML, its the name of the element. Commented Oct 8, 2011 at 20:01
  • Oh, I see. Sorry, I got that part wrong. Commented Oct 8, 2011 at 20:16

1 Answer 1

10

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! the first one did the trick. I haven't tried jQuery yet because I'm just starting to learn Javascript.
Then that's good, I recommend to learn the basic first (native Javascript) Then jQuery. I will be easier that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.