March 31, 2006
Using JavaScript to get radio values
A coworker was trying to figure out how to use JavaScript to get a RADIO value from a form. Because a radio form element makes up an array of possible selections, you have to cycle through that array to find the selected value. It is then that you can retrieve the actual selected value.
Check out the code below that I’m using to get the value of the selected radio element. From this you should be able to customize it for your needs. Simply insert your code or run your function in the level where I’m running the alert() command.
<script language="javascript">
function rockmyworld() {
i=0;
do {
if (form.myRadio[i].checked == true) {
alert(form.myRadio[i].value);
}
i++;
} while (i < form.myRadio.length);
}
</script>
<form name=”form”>
<input type=”radio” name=”myRadio” value=”1″/> 1 <br/>
<input type=”radio” name=”myRadio” value=”2″/> 2 <br/>
<input type=”radio” name=”myRadio” value=”3″/> 3 <br/>
<input type=”button” value=”get value” onClick=”rockmyworld()”/>
</form>
Hope that helps any folk out there that were burnin’ brain cells trying to figure this out.

March 31, 2006, 3:27 pm
Filed under: HTML, JavaScript
1 Comment »
RSS feed for comments on this post
Leave a Comment
You must be logged in to post a comment.


Jamison said,
March 31, 2006 @ 3:40 pm
Hehe… a year and a half too late for me