Click to See Complete Forum and Search --> : HOW TO: Checkboxes Dependent on RadioButton?


kwilliams
10-24-2003, 02:54 PM
I'm really hoping that someone knows how to do this, or can point me in the right direction. Basically, I have a form with 2 radiobuttons and 4 checkboxes. I want the 4 checkboxes selected if the first radiobutton is checked. This is what I came up with using javaScript:

<form action="searchresults.asp" name="form1">
<input type="radio" name="rq" value="0" onClick="if (this.checked) { document.form1.col.checked=true; document.form1.col.checked=true; document.form1.col.checked=true; document.form1.col.checked=true;}">
<input type="checkbox" name="col" value="dgcoweb">Site-wide&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="col" value="agendas">Agendas&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="col" value="minutes">Minutes&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="col" value="newsrel">News Releases
</form>

...but it doesn't work. But I also don't get any error messages. Can anyone see what I'm doing wrong? Thanks.

KWilliams

Jona
10-24-2003, 03:06 PM
<input type="radio" name="rq" value="0" onClick="if(this.checked){ for(i=0; i<4; i++){this.form.col[i].checked=true;}}">


Since the checkboxes have the same name, they are treated as an array.

[J]ona

kwilliams
10-24-2003, 03:16 PM
Hi Jona (great pic),

It worked great, but there's one more thing. When I refreshed the page, the radiobutton was selected, and the checkboxes weren't. It wasn't until I clicked the other radiobutton, and then clicked the referenced radiobutton that the 4 checkboxes became selected. Is there something else that I need to add into the code to make them selected on the load ofthe page? Thanks Jona.

KWilliams

kwilliams
10-24-2003, 03:20 PM
Jona,

I figured out the simple solution on my own. I just needed to add that code to the other radiobutton, except I changed it to "checked=false". I then made all of the checkboxes "checked" on load. Thanks for your quick response. I really appreciate it! Have a great weekend.

KWilliams

Jona
10-24-2003, 03:22 PM
What happened, if you're interested, is that the data remained in cache so the page would load more quickly. To prevent this, hold Shift and click the Refresh button on your browser. (The method you have used it a bit more reliable, though, as users may decide to reload the page.)

Thanks for the complement on my avatar, :D
[J]ona