Solved

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

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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

623 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