Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select corresponding radio button by value

Posted on 2006-10-19
2
Medium Priority
?
195 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

916 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