I want to get the selected value from a group of radio buttons.
Here\'s my HTML:
If you are using jQuery:
$('input[name="rate"]:checked').val();
check value by ID:
var CheckedValues = ($("#r1").is(':checked')) ? 1 : 0;
This works in IE9 and above and all other browsers.
document.querySelector('input[name="rate"]:checked').value;
My take on this problem with pure javascript is to find the checked node, find its value and pop it out from the array.
var Anodes = document.getElementsByName('A'),
AValue = Array.from(Anodes)
.filter(node => node.checked)
.map(node => node.value)
.pop();
console.log(AValue);
Note that I'm using arrow functions. See this fiddle for a working example.
Simply use: document.querySelector('input[rate][checked]').value
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;
}
}