Solved

How to get the selected value in a radiobutton in javascript?

Posted on 2010-11-07
3
267 Views
Last Modified: 2012-08-13
Hello,

I have 4 radio buttons (all radiobuttons have the same "name" & same "id" but diff. "values")with a normal button beside it. When I click on the normal button, I want to be able to get the selected radiobutton's value through a function call. How do I do that in javascript?
0
Comment
Question by:dustbunnies
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
sam2912 earned 500 total points
ID: 34082322

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Get Radio Button Value</title>
<script type="text/javascript">
function getRadio(radioName) {
	var radios = document.getElementsByName(radioName);
	
	for(var i=0; i<radios.length; i++) {
		if(radios[i].checked == true) return radios[i].value;
	}
}
</script>
</head>

<body>
<input name="radio" id="radio" type="radio" value="1" /><br />
<input name="radio" id="radio" type="radio" value="2" /><br />
<input name="radio" id="radio" type="radio" value="3" /><br />
<input name="" type="button" value="Get Value of Selected Radio" onclick="alert( getRadio('radio') );" />
</body>
</html>

Open in new window

0
 
LVL 14

Expert Comment

by:sam2912
ID: 34082363
By the way, id should be unique.
0
 

Author Closing Comment

by:dustbunnies
ID: 34082422
Thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

757 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

21 Experts available now in Live!

Get 1:1 Help Now