Solved

Select corresponding radio button by value

Posted on 2006-10-19
2
187 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
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now