I’m having some strange problem with my JS program. I had this working properly but for some reason it’s no longer working. I just want to find the value of the radio button (which one is selected) and return it to a variable. For some reason it keeps returning undefined
.
Here is my code:
function findSelection(field) {
var test = 'document.theForm.' + field;
var sizes = test;
alert(sizes);
for (i=0; i < sizes.length; i++) {
if (sizes[i].checked==true) {
alert(sizes[i].value + ' you got a value');
return sizes[i].value;
}
}
}
submitForm
:
function submitForm() {
var genderS = findSelection("genderS");
alert(genderS);
}
HTML:
<form action="#n" name="theForm">
<label for="gender">Gender: </label>
<input type="radio" name="genderS" value="1" checked> Male
<input type="radio" name="genderS" value="0" > Female<br><br>
<a href="javascript: submitForm()">Search</A>
</form>
Using a pure javascript, you can handle the reference to the object that dispatched the event.
JavaScript:
http://jsfiddle.net/Xxxd3/610/
Edit: Thanks HATCHA and jpsetung for your edit suggestions.
The simplest solution:
Here is an Example for Radios where no Checked="checked" attribute is used
DEMO : http://jsfiddle.net/ipsjolly/hgdWp/2/ [Click Find without selecting any Radio]
Source : http://bloggerplugnplay.blogspot.in/2013/01/validateget-checked-radio-value-in.html
etc then use just
Or
This works with any explorer.
It is the purest way to get the value of any input type. You do not need to include jQuery path as well.