radio on when checkbox checked

J

John Mullen

I want to take the following HTLM and use javascript to turn on radio buttons if checkbox is checked, can I do this with javascript (maybe onClick or an array) or do i need a server side script ?
<li>ABACAVIR SULFATE</li>
<INPUT NAME="ingredient0" TYPE=checkbox VALUE="ABACAVIR SULFATE"><br>
<input name="ABACAVIR SULFATE AMOUNT" type="radio" value="EQ 300MG BASE">EQ 300MG BASE<br>
<input name="ABACAVIR SULFATE AMOUNT" type="radio" value="EQ 20MG BASE/ML">EQ 20MG BASE/ML<br>
 
M

Michael Winter

John Mullen wrote on 21 Dec 2003 at Sun, 21 Dec 2003 10:41:13 GMT:
I want to take the following HTLM and use javascript to turn on
radio buttons if checkbox is checked, can I do this with
javascript (maybe onClick or an array) or do i need a server side
script ? <li>ABACAVIR SULFATE</li>
<INPUT NAME="ingredient0" TYPE=checkbox VALUE="ABACAVIR
SULFATE"><br> <input name="ABACAVIR SULFATE AMOUNT" type="radio"
value="EQ 300MG BASE">EQ 300MG BASE<br> <input name="ABACAVIR
SULFATE AMOUNT" type="radio" value="EQ 20MG BASE/ML">EQ 20MG
BASE/ML<br>

You can do this with JavaScript, but the method differs depending on
whether you use forms to contain the controls.

With forms:

<FORM ... name="form_name">
...
<INPUT ... type="checkbox"
onclick="enableGroup('radio_name',this.form,this.checked)">
<INPUT ... name="radio_name" type="radio">
<INPUT ... name="radio_name" type="radio">
...
</FORM>

<SCRIPT type="text/javascript">
// Enables or disables a group of controls that share the same
// name
//
// groupName - string that contains the name of the control group
// form - the form object that contains the controls
// enable - boolean that if true, enables the group. If false,
// the group is disabled
//
function enableGroup(groupName, form, enable) {
// Get the group of radio buttons.
var group = form.elements[groupName];
var numControls = group.length;

// Loop through the collection enabling (or disabling) the
// controls.
for (var i = 0; i < numControls; ++i) {
group.disabled = !enable;
}
}
</SCRIPT>


With or without:

<INPUT ... type="checkbox"
onclick="enableGroup('radio_name',this.checked)">
<INPUT ... name="radio_name" type="radio">
<INPUT ... name="radio_name" type="radio">

<SCRIPT type="text/javascript">
// Enables or disables a group of controls that share the same
// name
//
// groupName - string that contains the name of the control group
// enable - boolean that if true, enables the group. If false,
// the group is disabled
//
function enableGroup(groupName, enable) {
var group = null;

// Attempt to get the group of radio buttons. First try the DOM
// approach, then try Microsoft proprietary if DOM is not
// supported.
if (document.getElementsByName) {
group = document.getElementsByName(groupName);
} else if (document.all) {
group = document.all[groupName];
}

// If the group was obtained successfully, loop through the
// collection enabling (or disabling) the controls.
if (group) {
var numControls = group.length;

for (var i = 0; i < numControls; ++i) {
group.disabled = !enable;
}
}
}
</SCRIPT>

The examples above can be used to enable or disable any form control,
not just radio buttons. The examples also assume that you want the
controls enabled when the checkbox is ticked. If you need them to be
disabled when the box is ticked, use this:

<INPUT ... type="checkbox"
onclick="enableGroup('radio_name',!this.checked)">

Note that an exclamation mark (!) is used here (to invert the boolean
value) and is not in the example controls above.

<snipped HTML format post>

You should only use text to post to Usenet. Please don't use HTML.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top