radio on when checkbox checked

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Mullen

    radio on when checkbox checked

    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="ingredien t0" 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>

  • Michael Winter

    #2
    Re: radio on when checkbox checked

    John Mullen wrote on 21 Dec 2003 at Sun, 21 Dec 2003 10:41:13 GMT:
    [color=blue]
    > 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="ingredien t0" 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>[/color]

    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="enable Group('radio_na me',this.form,t his.checked)">
    <INPUT ... name="radio_nam e" type="radio">
    <INPUT ... name="radio_nam e" 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(gro upName, 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[i].disabled = !enable;
    }
    }
    </SCRIPT>


    With or without:

    <INPUT ... type="checkbox"
    onclick="enable Group('radio_na me',this.checke d)">
    <INPUT ... name="radio_nam e" type="radio">
    <INPUT ... name="radio_nam e" 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(gro upName, 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.getEl ementsByName) {
    group = document.getEle mentsByName(gro upName);
    } 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[i].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="enable Group('radio_na me',!this.check ed)">

    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

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

    Comment

    Working...