Radio buttons

Discussion in 'Javascript' started by Laser Lips, Aug 22, 2008.

  1. Laser Lips

    Laser Lips Guest

    Hi All. This one has plagued me for a while.

    I want to get the value of the selected radio button in 2 ways.
    Bearing in mind that the radio buttons are always in a form tag.

    1) I want to be able to get the value by using somthing like the
    following...

    myRadio = document.getElementById("myRadio");
    //then somehow get the value of the selected radio button.

    2) Have a function which is pased the form element and get to it that
    way. eg.

    function getVals(form)
    {
    myrad = form.myRadio
    //then somehow get the value of the selected radio button.
    }

    =========================

    Playing with other code i was able to get the value by doing the
    following...

    <HTML>
    <HEAD>
    <script>
    function GO()
    {
    alert(rad.length);
    for(x=0;x<rad.length;x++)
    {
    if(rad[x].checked==true)
    {
    alert(rad[x].value);
    }
    }
    }
    </script>
    </HEAD>
    <BODY>
    <input type='radio' name='rad' id='rad' value='answer1'/>1
    <input type='radio' name='rad' id='rad' value='answer2'/>2
    <input type='radio' name='rad' id='rad' value='answer3'/>3
    <input type='radio' name='rad' id='rad' value='answer4'/>4
    <button onclick='GO();'>GO</button>
    </BODY>
    </HTML>

    =========================

    BUT I dont want to be able to just access the radio button direclty
    because in some applications of this code, it doesnt work.

    I really want to use document.getElementById and then continue from
    that.

    Any suggestions?
    Graham
     
    Laser Lips, Aug 22, 2008
    #1
    1. Advertisements

  2. Laser Lips

    Laser Lips Guest

    Never mind, if I get the form by doing a document.getElementById on
    the form and then using the . operator like this, it works....

    theForm = document.getElementById("myForm")
    alert(theForm.myRadio.length);


    Graham
     
    Laser Lips, Aug 22, 2008
    #2
    1. Advertisements

  3. Laser Lips

    Evertjan. Guest

    Laser Lips wrote on 22 aug 2008 in comp.lang.javascript:
    Illegal.

    id's must be unique.
     
    Evertjan., Aug 22, 2008
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.