Get value from a button radio

0

How do I get the values from the radio button? I tried with the code below, but it only returns me the value of the first one:

<input type='radio' name='Seguro' id="seguro" onchange="soma()" value='Sim'> Sim
<input type='radio' name='Seguro' id"seguro" onchange="soma()" value='Não'> Não

And the javascript (test):

function soma(){

....
alert(document.getElementById("seguro").value);
....

}
    
asked by anonymous 27.08.2015 / 20:51

2 answers

1

You can use this solution that is in the StackOverflow English .

var choices = [];
var els = document.getElementsByName('choice_shrd_with_me');
for (var i=0;i<els.length;i++){
  if ( els[i].checked ) {
    choices.push(els[i].value);
  }
}
    
27.08.2015 / 20:53
0

In your case, the two inputs have the same id, you can change the id, or pass the element itself as a parameter of the function so = onchange="soma(this)"; and then change its js to receive the parameter.     

27.08.2015 / 20:55