Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2003
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I determine which button has been selected in a set of radio buttons?

    In this code:

    document.form.radio[?].value

    What can I put in place of the question mark to tell me which button the user has selected?

  • #2
    New Coder
    Join Date
    Oct 2003
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry can i rephrase the question more clearly.

    I have a basic script that, when someone clicks a radio button, displays something in a text form input.
    I have:
    Code:
    document.form_3.radfv.value=document.form_3.radfs[].value
    form_3 is the name of the form i'm using, radfv is the name of the input where I want to display the infomation, and radfs is the name of the set of radio buttons. How do determine which button the user has selected instead of putting in a absolute value, ie
    Code:
    radfs[something]
    instead of
    Code:
    rdfs[0]

  • #3
    New Coder
    Join Date
    Oct 2003
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Good question - tough answer

    I just had to struggle with this myself, so I'm glad to know I'm not the only one learning this stuff! This is only my solution to this though and there may be an easier way.

    Because radio buttons with the same name are treated as an array, I had to create a function that would check each one to see which one was checked and then return that value.

    Ex.
    for (i=0; i<document.form_3.radfs.length;i++){
    if (document.form_3.radfs[i].checked==true){
    valuedesired=document.form_3.radfs[i].value;}
    }

    "valuedesired" would then contain the selected radio button value, so then you could:

    document.form_3.radfv.value=valuedesired;

    Unless the output is going to something besides an element that will display text.

    Hope this helps...
    Does anyone else have another solution? I would like to know of a better way to do this myself!
    Last edited by xupaosso; 10-19-2003 at 05:27 AM.

  • #4
    New Coder
    Join Date
    Oct 2003
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks

    Thanks for the help, that will be really useful

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this one.


    <head>
    <script type="text/javascript">
    function validateRadio(form1) {
    thisOne = -1;

    for (i=0; i<form1.radio1.length; i++) {
    if (form1.radio1[i].checked) {
    thisOne = i;
    whichOne = (thisOne+1);
    }
    }
    if (thisOne == -1) {
    alert("You must select a radio button!");
    return false;
    }
    else {
    alert("You selected: " +form1.radio1[thisOne].value+ "\n Radio Button: " + whichOne);
    return true;
    }
    }
    </script>
    </head>
    <body>
    <form name=form1>
    <input type=radio value="Red" name=radio1>1
    <input type=radio value="Yellow" name=radio1>2
    <input type=radio value="Green" name=radio1>3
    <input type=button value="Validate" onclick="validateRadio(form1)";>
    </form>
    </body>
    </html>


    .....Willy

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not sure if any of this is helpful, but
    I added a variable to capture the ID.

    <script language="javascript">
    function validateRadio(form1) {
    thisOne = -1;

    for (i=0; i < form1.radio1.length; i++) {
    if (form1.radio1[i].checked) {
    thisOne = i;
    whichOne = (thisOne+1);
    idIt=document.getElementsByTagName("INPUT")[i].id
    }
    }
    if (thisOne == -1) {
    alert("You must select a radio button!");
    return false;
    }
    else {
    alert("You selected: " +form1.radio1[thisOne].value+ "\n Radio Button: " +whichOne+ "\nThe ID of Radio Button "+whichOne+" is: "+idIt);
    return true;
    }
    }
    </script>
    </head>
    <body>
    <form name=form1>
    <input type=radio value="Red" name=radio1 ID="My Red Button">1
    <input type=radio value="Yellow" name=radio1 ID="My Yellow Button">2
    <input type=radio value="Green" name=radio1 ID="My Green Button">3
    <input type=button value="Validate" onclick="validateRadio(form1)";>
    </form>


    ....Willy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •