if you are using javascript or an ajax web form, you might find yourself wondering how you can get the correct value of the radio buttons on your form. because when you submit the form, the value that was not checked, is not the correct value of the radio button thats being sent on your javascript.

well, if you are in the same situation, you can write a javascript function to check whether the field is a button or not, and if it is a radio button type, you can get the value and send it to wherever you need to.

so i created this short script to show you how it works, you can create the script yourself, or you can downloaded it from this post. 1. first open your favorite text editor. i am using windows so i am going to be using notepad. open a blank notepad and copy and paste the following code:

<!--
############## CREATED BY WWW.WEBUNE.COM ###############
############## COPYRIGHT UGN/GPL 2010 #################
############# PLEASE DO NOT REMOVE THIS ###############
-->
<p>This small script shows you how to get the javascript value in a web form.<br /></p>
<script type="text/javascript">
<!--

function RadioButtonValue(){
	for (var i=0; i < document.WebuneForm.color.length; i++){
		if (document.WebuneForm.color[i].checked){
			return document.WebuneForm.color[i].value;
		}
	}
}
function ShowValue(){
	var RadioValue = RadioButtonValue();
	alert('You Selected the Color: ' + RadioValue);
}
//-->
</script>

<form name="WebuneForm">
Whats your favorite color?<br>
<input type="radio" name="color" value="Red" checked="checked">Red<br>
<input type="radio" name="color" value="Blue">Blue<br>
<input type="radio" name="color" value="Yellow">Yellow<br>
<input type="radio" name="color" value="Black">Black<br>
<input type="radio" name="color" value="Green">Green<br>
<input type="button" onclick="ShowValue()" value="Check">
</form>

<p><img src="http://www.webune.com/images/headers/default_logo.jpg" /></p>
<p>Script Created by www.<a href="http://www.webune.com">Webune.com</a></p>


2. now save the file as radio-button.html - once you have save it, open it with your browser and see it in action.

DEMO:

Whats your favorite color?
Red
Blue
Yellow
Black
Green
radio-button.html