Javascript Selected Radio
And element's IDs must be different.
Solution 2:
To get the value of the checked radio button, without jQuery:
var radios = document.getElementsByName("u_type");
for(var i = 0; i < radios.length; i++) {
if(radios[i].checked) selectedValue = radios[i].value;
}
(assuming that selectedValue
is a variable declared elsewhere)
Solution 3:
$('input[name=u_type]:checked').val()
will get you the value of the selected option which you can, of course, assign to a variable. Due to admonishment, I should also point out that this is jquery, a handy javascript library for making DOM manipulation easier and with excellent cross-browser compatibility. It can be found here.
Solution 4:
Alternatively to kmb385's suggestion you could wrap your inputs in a form, and make sure all of the input names are different (you have two u_type) names.
Then you can access the inputs as document.formname.inputname.checked
, which will return true or false.
Solution 5:
I know this is an old question, but the answers given seem overly complex.
As pointed out earlier, you should not have two elements with the same id. That violates the html spec. Just remove the id attributes, or make them two different values. Then simply use this to get the checked value:
document.querySelector("input[type='radio'][name='u_type']:checked").value
Post a Comment for "Javascript Selected Radio"