www.webdeveloper.com
Results 1 to 3 of 3

Thread: Radio button question

  1. #1
    Join Date
    Oct 2005
    Posts
    22

    Radio button question

    I'm a javascript newb so this is probably pretty basic, but I'm having a problem with processing radio buttons. I have a series of radio buttons on the a form which I want to loop through and verify that none of the options are checked.

    Let's say that I have radio buttons called btn1, btn2, and btn3 and each has two options. I then set a for loop (we'll say I'm using the variable 'c') to process through the buttons.

    My first attemp was to try this:

    mybtn=document.getElementById(btn+c)

    but it seemed to get a handle on the first option in the button rather than the group, so I changed it to this:

    btnGrp = document.getElementById(btn+c).parentElement

    I then try to process through the options with this code:

    for (var x=0;x<btnGrp.childNodes.length;x++){
    btnGrp.childNodes[x].checked = false
    }


    The problem with this is that while there are two options for my radio button, there are 5 child nodes and not all of them have the 'checked' property, so my code chokes.

    Long stroy short, what's the best way to process through a series of radio buttons and make sure that all options are unchecked?

  2. #2
    Join Date
    Sep 2005
    Posts
    48
    There isn't a "Button Group Element". What you have to do is give your radio buttons which are grouped the same name
    Code:
    1 <input type="radio" name="test" value=1 checked="true">
    2 <input type="radio" name="test" value=2>
    3 <input type="radio" name="test" value=3>
    Then, to loop through the options do this:
    Code:
    var obj = document.getElementsByName("test");
    for(var i=0;i<obj.length;i++)
    {
    obj[i].checked = false;
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    22
    Awsome! I knew it had to be something simple: getElementsByName instead of getElementbyId.

    Thanks!

    When I reffered to the button "group", I was talking about the ability to access each radio choice as name[x] - I expected to do that with the getElementById like you can with getElementsByName.
    Last edited by Girevik; 10-25-2005 at 02:10 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles