Javascript - I can not find my error (radio buttons)

I want to create an online test, but something is wrong in my first question. The code below does not work and returns "your score is 0", even when the correct answer is checked.

<!DOCTYPE html>
<html>sssdsdsdsd

<head>Mypage.com
<title> myPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>

<script>
var total=0;
var yourmark="your score is  ";

if(document.getElementById('q12').checked) {
  total=total+1;
}else {
 total=total;
}

</script>

<script>
function showScore() {
    alert(yourmark + total  );
}
</script>
</br>
<input type="radio" name="question1" id="q11" value="false">Question 1-first option <br>
<input type="radio" name="question1" id="q12"  value="true" > Question 1- second option<br>
<input type="radio" name="question1" value="false">Question 1- third option<br>



<button onclick="showScore()">Show my score</button>



</body>
</html>
Run codeHide result
+4
source share
3 answers

http://codepen.io/anon/pen/vgJuA

So, leave var total = 0 as a global variable outside functions and create a function checkAnswer(). Now your javascript part will be:

var total = 0;    
function checkAnswer() {
    //total is the global Variable for the score
    if (document.getElementById('q12').checked) {
        total = total + 1;
    } else {
        total = total;
    }
    return total;    
}

function showScore() {
    var yourmark = "your score is  ";
    alert(yourmark + checkAnswer());
}

Hope this helps!

+1
source

The following are the direct reasons why you are not working the way you want.

  • total , . HTML . script .

  • , value.

- .

<!DOCTYPE html>
<html>sssdsdsdsd

<head>Mypage.com
<title> myPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>

<script>
function showScore() {
    alert(yourmark + total  );
}
</script>
</br>
<input type="radio" name="question1" id="q11">Question 1-first option <br>
<input type="radio" name="question1" id="q12"  checked="true" > Question 1- second option<br>
<input type="radio" name="question1">Question 1- third option<br>



<button onclick="showScore()">Show my score</button>


<script>
var total=0;
var yourmark="your score is  ";

if(document.getElementById('q12').checked) {
  total=total+1;
}else {
 total=total;
}

</script>


</body>
</html>
Hide result
+2

:

var total=0;
var yourmark="your score is  ";

if(document.getElementById('q12').checked) {
  total=total+1;
}else {
 total=total;
}

, . total, , 0. , , , "total . At no point are you updating the value of total" .

, PM 77-1, , total , .

<!DOCTYPE html>
<html>sssdsdsdsd

<head>Mypage.com
<title> myPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>

<script>
function showScore() {
  var total=0;
  var yourmark="your score is  ";

  if(document.getElementById('q12').checked) {
    total=total+1;
  }else {
   total=total;
  }
  
  alert(yourmark + total  );
}
</script>
</br>
<input type="radio" name="question1" id="q11" value="false">Question 1-first option <br>
<input type="radio" name="question1" id="q12"  value="true" > Question 1- second option<br>
<input type="radio" name="question1" value="false">Question 1- third option<br>



<button onclick="showScore()">Show my score</button>



</body>
</html>
Run codeHide result
+1
source

All Articles