Solved

Select corresponding radio button by value

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

773 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