Page 1 of 1

Checkbox (Check All, Uncheck All)

Posted: Thu Jan 15, 2004 12:59 am
by phppick
Hi

I got a function like this

function setCheckboxes(the_form, do_check)
{
var elts = (typeof(document.forms[the_form].elements['c[]']) != 'undefined')
? document.forms[the_form].elements['c[]']
: (typeof(document.forms[the_form].elements['c[]']) != 'undefined')
? document.forms[the_form].elements['c[]']
: document.forms[the_form].elements['c[]'];
var elts_cnt = (typeof(elts.length) != 'undefined')
? elts.length
: 0;

if (elts_cnt) {
for (var i = 0; i < elts_cnt; i++) {
elts.checked = do_check;
} // end for
} else {
elts.checked = do_check;
} // end if... else

return true;
} // end of the 'setCheckboxes()' function

To Check All Checkboxes and Uncheck All in a Form.. But this Funtion is not working when there are multiple checkboxes..

And Checkbox like this

<input type="checkbox" value="checked" id="c[]" name="c[0]">
<input type="checkbox" value="checked" id="c[]" name="c[1]">
<input type="checkbox" value="checked" id="c[]" name="c[2]">

Anybody can help me??

Posted: Thu Jan 15, 2004 2:34 pm
by Unipus
Try something like this:

Code: Select all

var theFormElems = document.getElementsByTagName('input');
for (var i = 0; i < theFormElems.length; ++i) 
&#123;
     if (theFormElems&#1111;i].type == 'checkbox' && theFormElems&#1111;i].checked)
     &#123;
          theFormElems&#1111;i].checked = false;
      &#125;
&#125;
And then obviously the reverse, as well. You actually don't even need that second check (&& theFormElems.checked), but it might be useful at some point.