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;
0

2 Answers 2

2

Try this (using querySelector in JS) :

function getSel() {
var formInput = document.getElementById("numberForm");
var rb=formInput.querySelector('[type="radio"]:checked'); //get selected radio button
document.getElementById('spVl').innerHTML = 'Selected value = '+rb.value;
}
<form id="numberForm">
<input type="radio" name="number" value="singular"> singular <br>
<input type="radio" name="number" value="plural"> plural <br>
<input type="button" value="Get selected radio button value" onclick="getSel();" /><br>
<span id="spVl"></span>
</form>

Sign up to request clarification or add additional context in comments.

Comments

0
var form = document.getElementById('numberForm');
var radios = form.getElementsByTagName('input');

for(var i = 0; i < radios.length; ++i) {
  if(radios[i].checked) {
    console.log("Radio button '" + radios[i].value + "' is checked");
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.