You are here: Home » JavaScript » How to get value of selected radio button in Javascript?

How to get value of selected radio button in Javascript?

5 Flares 5 Flares ×

Today i am going to talk about, How to get value of selected radio button in Javascript? To get value of selected radio button, first define different IDs for options in HTML section. On submit button call a function, here i am calling a function validate();. You will get selected radio value in variable gen_type.
HTML Part

<table >
<form>
<tr><td>I am </td><td><input type="radio" id="gender1" name="gender" value="Male"> Male
<input type="radio" id="gender2" name="gender" value="Female"> Female</td></tr>
<table><tr><td colspan="2"><input type="submit" name="submit" value="Save" onClick="return validate();"/></td></tr>

</form>
</table>

Javascript Part

<script type="text/javascript">
function validate()
{
if (document.getElementById('gender1').checked) {
  var gen_type = document.getElementById('gender1').value;
}
if (document.getElementById('gender2').checked) {
  var gen_type = document.getElementById('gender2').value;
}
}
</script>

You will find value of selected radio button in gen_type.

About Abhishek Sinha

I am Abhishek and I love to code on PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Scroll To Top
5 Flares Twitter 0 Facebook 0 Google+ 3 StumbleUpon 1 Pin It Share 0 LinkedIn 1 5 Flares ×