input[name="gndr"]:checked is the selector, it means select all the input tag and its name attribute value should gndr.
.value mean selected html element value attribute value is return.
Code:-
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Get Radio Button Value using name</h1>
<label><input type="radio" name="gndr" value="1">Male</label>
<label><input type="radio" name="gndr" value="2">FeMale</label>
<br>
<input type="button" onclick="chk_ans();" value="check">
<script>
function chk_ans()
{
var val = document.querySelector('input[name="gndr"]:checked').value;
alert(val);
}
</script>
</body>
</html>
Get radio value using name:- JavaScript
Reviewed by
Network security
on
November 13, 2019
Rating:
5
No comments: