Solved

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

Posted on 2010-11-07
3
270 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

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.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

839 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