We help IT Professionals succeed at work.

Checkbox JavaScript

DuffeyP
DuffeyP used Ask the Experts™
on
I have the following script where one checkbox selects a column of radio buttons (Radio0) and another de-selects it. I would like to combine these into a single checkbox where checking selects and unchecking de-selects it.

Here is the JS code I have now.

<script>

//Check all radio/check buttons script- by javascriptkit.com
//Visit JavaScript Kit (http://javascriptkit.com) for script
//Credit must stay intact for use  

function checkall(formname,checkname,thestate){
var el_collection=eval("document.forms."+formname+"."+checkname)
for (c=0;c<el_collection.length;c++)
el_collection[c].checked=thestate
}

</script>

And here is the code for the 2 checkboxes that select the column of radios. I would like to combine these into a single checkbox that both selects and de-selects.

<input type="checkbox" name="All" value="0" onClick="javascript:checkall('theform','Radio0',true)">

<input type="checkbox" name="All" value="0" onClick="javascript:checkall('theform','Radio0',false)">
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Principal Data Engineer
Commented:
You need to change your function to check if they are checked or not. Inside your loop you should use something like

if(el_collection[c].checked) {
// the box is checked so uncheck it
el_collection[c].checked=false;
} else {
el_collection[c].checked=true;
}

You can do away with the 'theState' in the function now as this will now act as a toggle to check or un-check them. So to call it would be like this

<input type="checkbox" name="All" value="0" onClick="javascript:checkall('theform','Radio0')">

Don't forget to remove 'theState' from your function.

Hope this helps

Author

Commented:
Thanks - that did the trick. Appreciate the quick response.
Mark BradyPrincipal Data Engineer

Commented:
You're welcome and thanks for the points