0

I have a form on my webpage that looks like this:

<form id="numberForm">
<input type="radio" name="number" value="singular"> singular <br>
<input type="radio" name="number" value="plural"> plural <br>
</form>

How do I pull the value of the currently selected radio button (without a submit action) in Javascript? What I'd like is something along the lines of the following:

var formInput = document.getElementById("numberForm");
var numberInputValue = formInput.SELECTEDBUTTON.value;
sitian Gu
  • 9
  • 1

1 Answers1

1

very close, you just need to use the name of the radio button group in place of "SELECTEDBUTTON", in this case "number":

document.getElementById('btn').addEventListener('click', function() {
  var formInput = document.getElementById("numberForm");
  var numberInputValue = formInput.number.value;
  alert(numberInputValue);
});
<form id="numberForm">
  <input type="radio" name="number" value="singular"> singular <br>
  <input type="radio" name="number" value="plural"> plural <br>
  <button id="btn">Click</button>
</form>
Rhumborl
  • 16,349
  • 4
  • 39
  • 45