SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Radio Button Group Value

    Ok, so in all my time doing web stuff I have never needed a radio button group, but now I do and I need help.

    I have 2 Radio Buttons like so:

    Code:
    <input id="D1" type="radio" name="Department" value="D1" tabindex="1" />
    	<label for="D1"><font size="+2" color="#333399">D1</font></label>
    	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input id="D2" type="radio" name="Department" value="D2" tabindex="1" />
    	<label for="D2"><font size="+2" color="#333399">D2</font></label>
    Now I want to make sure one of the two is selected.

    I tried in JS to do:

    Code:
    document.getElementById("D1").selected == true;
    //and
    document.getElementById("D1").value == true;
    //and
    document.getElementById("D1").value
    But the first gives me undefined and the second simply false, because the third yields "D1".

    I also tried .Selected, but that again just gave undefined wether or not D1 was selected.

    Please help,
    Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  2. #2
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You've almost got it. Instead of seeing if its 'selected' you want to see if its 'checked'.

    Code:
    document.getElementById("D1").checked == true;

  3. #3
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Re: Radio Button Group Value

    Close!

    Radio buttons are arrays in JS. You can do this (assumes your form is named "form1":
    Code:
    document.form1.Department[0].checked
    to see if D1 is checked, or:
    Code:
    document.form1.Department[1].checked
    To see if D2 is checked.

  4. #4
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, the code I posted works. I tested it. Thats not to say that thats how I would do it (probably do it more like vgarcia and loop through them). My solution was based on the code posted by riddelrp.

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks guys,

    I only have 2 (only will ever have 2) on the page, so I just checked each one ala
    Code:
    document.getElementById("D1").checked == true;
    since (to me at least) it is a little more obvious exactly what is going on in the code.

    Thanks again,
    Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.


Bookmarks

Posting Permissions

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