Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value of selected radio button?

I want to get the selected value from a group of radio buttons.

Here's my HTML:

<div id="rates">   <input type="radio" id="r1" name="rate" value="Fixed Rate"> Fixed Rate   <input type="radio" id="r2" name="rate" value="Variable Rate"> Variable Rate   <input type="radio" id="r3" name="rate" value="Multi Rate" checked="checked"> Multi Rate   </div> 

Here's my js:

var rates = document.getElementById('rates').value; var rate_value; if(rates =='Fixed Rate'){     rate_value = document.getElementById('r1').value;      }else if(rates =='Variable Rate'){     rate_value = document.getElementById('r2').value;      }else if(rates =='Multi Rate'){     rate_value = document.getElementById('r3').value; }    document.getElementById('results').innerHTML = rate_value; 

I keep getting undefined.

like image 941
ZombieBatman Avatar asked Apr 05 '13 16:04

ZombieBatman


People also ask

How do I get the value of a radio button in Python?

Syntax: button = Radiobutton(master, text=”Name on Button”, variable = “shared variable”, value = “values of each button”, options = values, …) value = each radiobutton should have different value otherwise more than 1 radiobutton will get selected.


1 Answers

This works in IE9 and above and all other browsers.

document.querySelector('input[name="rate"]:checked').value; 
like image 145
Parthik Gosar Avatar answered Sep 20 '22 15:09

Parthik Gosar