Helping ordinary people create extraordinary websites!
   

How can I control a group of checkboxes with only 1 check?

Wednesday, 26th August 2009
by lrhuot


How do I create a function that does the following:
1 - Loops thru a group of checkboxes.
2 - Unchecks all the ones except the one checked
3 - Allows for different group names to be used.
Thanks





bbrainz
Hello,

If you are looking for javascript you can use something like this.

Use the below function on change event for your button, you can select a mode as given below:

//int mode
// mode = 0 invert selection
// mode = 1 select none
// mode = other than 0 or 1, select all

function checkChange(mode)
{
for(i=0;i<document.forms[0].elements.length;i++)
{
var ele = document.forms[0].elements[i];
if(ele.type == 'checkbox')
{
if(ele.disabled=false)
{
if(mode==0) //invert selection
ele.checked = !ele.checked;
else if(mode==1) //none
ele.checked = false;
else
ele.checked = true;
}
}
}
}




Thursday, 27th August 2009
Votes:
32
16

More JavaScript Help:
» Java script total beginner needs to link two fields
» Catching up events outside an iframe
» How to enable and check if JavaScript enabled?
» How to use JavaScript unload event?
» How to send post data to the server without loading a new page?
» JavaScript random number?
» JavaScript timer using JavaScript SetTimeout and SetInterval
» How can i control my check box?