TransWikia.com

Accessing value of HTML element with a switch statement

Stack Overflow Asked by t_gravell on December 20, 2021

Every time I run the code it comes back with Test7 as undefined. Can anyone help explain why that would be? I’m very new so it may be obvious. I’m trying to make it so when the values of each ‘Test#’ are added up, it runs into a switch code and returns a different movie depending on what your score is. This is my javascript code. Thanks for any help.

var movie = document.getElementById("Test7").value;
function addValues6(c) {
var x = document.getElementById("Test").value;
var y = document.getElementById("Test2").value;
var z = document.getElementById("Test3").value;
var a = document.getElementById("Test4").value;
var b = document.getElementById("Test5").value;
document.getElementById("Test6").value = c;

var total = parseInt(z) + parseInt(y) + parseInt(x) +
parseInt(a) + parseInt(b) + parseInt(c);
document.getElementById("Test7").innerHTML = total;

switch (movie) {
  case "19":
  movie = "greener grass";
  break;
}
}

document.getElementById("Test8").innerHTML = movie;`

and this is the end of the HTML, which includes the final section of the quiz as an example. The rest of the code is formatted the same with different questions.

<div id="questionBox6">
<h1 id="questionHeader">Question 6</h1>
<p>What's your favorite TV show?</p>
<form id="questionForm6">
<input type="radio" value="2" id="q6a1" name="q6" onclick="addValues6(this.value)">
  Portlandia<br>
<input type="radio" value="1" id="q6a2" name="q6" onclick="addValues6(this.value)">
  F is For Family<br>
<input type="radio" value="3" id="q6a3" name="q6" onclick="addValues6(this.value)">
  The Walking Dead<br>
<input type="radio" value="4" id="q6a4" name="q6" onclick="addValues6(this.value)">
  Cold Case Files<br>
</form>
<input type="button" value="Next Question" id="nextQuestion6">
</div>

<div id="resultsBox">
  <p id="resultsHeader">These are your results!</p>
  <p id="results">Good job.</p>
  <p>You earned a:<p id="Test8"></p>
</div>

<p id="Test"></p>
<p id="Test2"></p>
<p id="Test3"></p>
<p id="Test4"></p>
<p id="Test5"></p>
<p id="Test6"></p>
<p id="Test7"></p>

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP