Radio buttons

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laser Lips

    Radio buttons

    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.getEle mentById("myRad io");
    //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.lengt h);
    for(x=0;x<rad.l ength;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.getEle mentById and then continue from
    that.

    Any suggestions?
    Graham
  • Laser Lips

    #2
    Re: Radio buttons

    On Aug 22, 10:21 am, Laser Lips <loudsphi...@gm ail.comwrote:
    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.getEle mentById("myRad io");
    //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.lengt h);
    for(x=0;x<rad.l ength;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.getEle mentById and then continue from
    that.
    >
    Any suggestions?
    Graham
    Never mind, if I get the form by doing a document.getEle mentById on
    the form and then using the . operator like this, it works....

    theForm = document.getEle mentById("myFor m")
    alert(theForm.m yRadio.length);


    Graham

    Comment

    • Evertjan.

      #3
      Re: Radio buttons

      Laser Lips wrote on 22 aug 2008 in comp.lang.javas cript:
      <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
      >
      Illegal.

      id's must be unique.



      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      Working...