Alex F Alex F - 1 month ago 9
HTML Question

Radio button value displaying JS function doesn't work after legally changing variables names

I have coded a function

showRadioValue()
in JavaScript which displays a value of radio button. It perfectly worked. But after I changed names of variables (from
radVal
to
ans6
) and function's name to
checkQ6()
, it mysteriously stopped working! Why? Here is the code:

<html>
<head>
<title>Radio test</title>
</head>
<body>
<h2 style="text-align: center;">Question 6</h2>
<p style="text-align: center;">We _______ two dogs and a cat.</p>

<form name="option" style="text-align: center;">
<input type="radio" name="option" value="have">have<br>
<input type="radio" name="option" value="is having"> is having<br>
</form>

<p style="text-align: center;" ><button onclick="checkQ6();">Test radio</button></p>

<script>
/*
function showRadioValue() { // previous perfectly working function
var radVal = document.querySelector('input[name = "option"]:checked').value;
alert(radVal);
}
*/
function checkQ6() { // the function where just ans6 variable name and function name is changed and which does not work
var ans6 = document.querySelector('input[name = "option"]:checked').value;
alert(ans6);

</script>
</body>
</html>

Answer

Your function bracket is not closed. You can see these errors in your browser console.