How to get value of selected radio button?
This works in IE9 and above and all other browsers.
document.querySelector('input[name="rate"]:checked').value;
var rates = document.getElementById('rates').value;
The rates element is a div
, so it won't have a value. This is probably where the undefined
is coming from.
The checked
property will tell you whether the element is selected:
if (document.getElementById('r1').checked) { rate_value = document.getElementById('r1').value;}
Or
$("input[type='radio'][name='rate']:checked").val();
You can get the value by using the checked
property.
var rates = document.getElementsByName('rate');var rate_value;for(var i = 0; i < rates.length; i++){ if(rates[i].checked){ rate_value = rates[i].value; }}