Solved

Select corresponding radio button by value

Posted on 2006-10-19
2
192 Views
Last Modified: 2008-02-01
I set a (var rbtVal) in global and would like to automatically check
the corresponding radio button in the form onLoad

smtng like:
function setRbtEcho()
{
  var thisRbtn;

for (i=0;i<3;i++)
{
//I don't know how to loop trough the getElementById for radio buttons
if (document.getElementById('myRadioButtonEcho'[i]).value = rbtVal)
    {document.getElementById('myRadioButtonEcho'[i]).checked;}

 }

<body onLoad="setRbtEcho();">
<form>
<input type="radiobutton" id="myRadioButtonEcho" value="2b">
<input type="radiobutton" id="myRadioButtonEcho" value="3c">
<input type="radiobutton" id="myRadioButtonEcho" value="4e">
</form>
</dody>
0
Comment
Question by:JoeUS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
Morcalavin earned 500 total points
ID: 17768496
You can't loop through getElementById, because only one element is allowed to have it at a time.  It's unique.  You need something like getElementByTagName.  You could also youse getElementsByName.  It depends on if you want the radio buttons to have the same name(grouping) or not.

<script>
function setRbtEcho() {
var rbtVal = '3c';
inputs = document.getElementsByTagName('input');
for(var i=0; i < inputs.length; i++) {
        if(inputs[i].type == 'radio') {
               if(inputs[i].value == rbtVal) {
                        inputs[i].checked = true;
                }
        }
}
}
</script>

<body onLoad="setRbtEcho();">
<form>
<input type="radio" id="myRadioButtonEcho" value="2b">
<input type="radio" id="myRadioButtonEcho" value="3c">
<input type="radio" id="myRadioButtonEcho" value="4e">
</form>
</body>
0
 

Author Comment

by:JoeUS
ID: 17768857
Fantastic!

Thanks a lot for the info on the getElementById and for the example.
I ended up using the getElementsByName
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question