Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-11-07
3
Medium Priority
?
274 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

688 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