The Web Blinders logo

Programming

How to get HTML radio type input value

#Method 1

<label for="male">
  <input type="radio" value="1" name="gender" id="male"> Male
</label>  
<label for="female">
  <input type="radio" value="0" name="gender" id="female"> Female
</label>
To get the gender selected, we will use following JavaScript.
// change it as per your requirements                            
var nameAttribute=`gender`; 
// selected radio value
document.querySelector(`input[name='${nameAttribute}']:checked`).value; 

#Method 2

// your form element
var form = document.getElementById("formId");

// based on name attribute
var gender = form.gender.value; // returns selected option

// if name = "choice"
var choice = form.choice.value; // returns selected option

Need developers ?

if so, send a message.

thewebblinders@gmail.com

More Programming from our blog

SEARCH FOR ARTICLES