www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Radio Button Loop, help me understand?

  1. #1
    Join Date
    Aug 2009
    Posts
    12

    resolved [RESOLVED] Radio Button Loop, help me understand?

    I'm not quite understanding how to loop a large amount of radio buttons to become checked or not. I could probably copy paste some code but I really want to understand this.

    So on my document I have a large amount of radio buttons. Such as:

    Code:
    <table style="width: 100&#37;" Name="Test1">
    		<tr>
    			<td><a href="#" onclick="test()">Yes</a></td>
    			<td>No</td>
    			<td>DK</td>
    		</tr>
    		<tr>
    			<td><input type="radio" name="Radio1" value="Yes" id="rYes1" /></td>
    			<td><input type="radio" name="Radio1" value="No" id="rNo1"/></td>
    			<td><input type="radio" name="Radio1" value="DK" id="rDK1"/></td>
    		</tr>
    		<tr>
    			<td><input type="radio" name="Radio2" value="Yes" id="rYes2" /></td>
    			<td><input type="radio" name="Radio2" value="No" id="rNo2"/></td>
    			<td><input type="radio" name="Radio2" value="DK" id="rDK2"/></td>
    		</tr>
    
    ....	
    
    </table>
    So to select a single button I can do:
    document.testform1.elements.rNo1.checked=true;

    It seems like I should be able to create a loop that says:
    var i=1;
    while (i<=20)
    {
    document.testform1.elements.rNo[i].checked=true;
    i++;
    }

    This does not work and I'm pretty sure it is because I am not understanding
    document.testform1.elements.rNo[i].checked=true;
    Normally this would be an array but looking at other code i have no idea what this is saying in javascript.
    Any help would be appreciated! Thank You!

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259

    Lightbulb Something to try ...

    Can you try? ...
    Code:
    var i=1;
    while (i<=20) {
      document.getElementById('rNo'+i).checked = true;
      i++;
    }

  3. #3
    Join Date
    Aug 2009
    Posts
    12
    I'll see what happens when I try that. Thanks!

  4. #4
    Join Date
    Aug 2009
    Posts
    12
    That works, the getElementById allows for this functionality it seems while doing it by document.(formname).elements does not. Thank you!

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259
    You're most welcome.
    Happy to help.
    Good Luck!

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