Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Houston, Texas

    Unanswered: Get VALUE from checked radio button?

    Dang. I just can't remember the syntax to do this.

    My form is named "form1"
    My radio button group is named "contactMeBy"

    I want to get the value from the selected item so I can pass it to another field. I know there is a simple way to do this with Javascript, but my notes (and the book I got this tip from) are at home. Can anyone remember how to do it WITHOUT writing a function to loop through the array?

    E-mail: <input type="radio" name="contactMeBy" value="email">
    Phone: <input name="contactMeBy" type="radio" value="phone" checked>
    This line is in a function tied to the SUBMIT button:
    formContactMe = (document.all.form1.contactMeBy[something in here?].value);

    Help is GREATLY appreciated!

  2. #2
    Join Date
    Jan 2003

    JS or ASP

    If you submit the form the value of the group contactMeBy will be the one which is selected.
    The Request.Form("contactMeBy") will return "email" in case the email radio button is checked.

    If you want to access the values of the radios from the group contactMeBy into JS, you'll have to use the indexes of the items from the group; like document.all.form1.contactMeBy[0].value refers to the value of the first radio in the group; the returned value will be "email". The document.all.form1.contactMeBy[1].value returns "phone".
    You can loop through the items of the group using the length attribute of the group returning the number of children.
    For example:

    <script language="JavaScript">
    for (i = 0 ; i < document.all.form1.contactMeBy.length ; i++)
    alert(document.all.form1.contactMeBy[i].value + " is " +document.all.form1.contactMeBy[i].checked);

    I hope that helps.

  3. #3
    Join Date
    Nov 2002
    Houston, Texas


    Thanks for the detailed description. It was useful, but I had to adapt it. I didn't want to loop through it and see which was checked, I just wanted to return the VALUE of the one that was checked.

    I actually just hard-coded it all (had to get it out today). I just wrote a function that manually changes the variable when the radio button is clicked.

    Thanks again for the help, though. Ihave filed this info -- I'm sure I'll use it eventually.

Posting Permissions

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