Solved

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

Posted on 2010-11-07
3
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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