Alex F Alex F - 1 year ago 67
HTML Question

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

I have coded a function

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

<title>Radio test</title>
<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>

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

function showRadioValue() { // previous perfectly working function
var radVal = document.querySelector('input[name = "option"]:checked').value;
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;


Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download