Sunday, May 7, 2017

Most easy way to get radio button value with Javascript


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<label for="gender">Gender: </label>
<input type="radio" name="genderS" value="1" checked="checked"/>Male
<input type="radio" name="genderS" value="2" />Female


<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x =  document.querySelector('input[name="genderS"]:checked').value;
    document.getElementById("demo").innerHTML = x;
}
</script>

No comments:

Post a Comment

Total Pageviews