2

I want to return the output of the score from a three button radio button quiz. I have the ouput working when I use alert but as the quiz is contained within a modal I don't want a popup I just want to display the output as within the modal to make it look cleaner I'm using InnerHTML however when I click the button I now get no output. I've been experimenting with using JQuery to try and show the results and hide the button but this isn't helping either. Does anyone know how I can get the results to print to "results"?

Thanks

<script>
          
var answers = ["A","B","B"], 
    tot = answers.length;

function getCheckedValue( radioName ){
    var radios = document.getElementsByName( radioName ); // Get radio group by-name
    for(var y=0; y<radios.length; y++)
      if(radios[y].checked) return radios[y].value; // return the checked value
}

function getScore(){
  var score = 0;
  for (var i=0; i<tot; i++)
    if(getCheckedValue("question"+i)===answers[i]) score += 1; // increment only
  return score;
}

function returnScore(){
  print("Your score is "+ getScore() +"/"+ tot);
}
</script>

var answers = ["A","B","B"], 
    tot = answers.length;

function getCheckedValue( radioName ){
    var radios = document.getElementsByName( radioName ); // Get radio group by-name
    for(var y=0; y<radios.length; y++)
      if(radios[y].checked) return radios[y].value; // return the checked value
}

function getScore(){
  var score = 0;
  for (var i=0; i<tot; i++)
    if(getCheckedValue("question"+i)===answers[i]) score += 1; // increment only
  return score;
}

function returnScore(){
  document.getElementById("results").innerHtml ("Well done! You scored "+ getScore() +"/"+ tot);
  
}
</script>
 
                
                <p> You are only charged interest on the amount that is remaining at the end of the month<br>
                <input type="radio" name="question0" value="A"> True </radio> 
                <input type="radio" name="question0" value="B"> False </radio> <br><hr>
               </p>
               
               <p>I have to pay off the balance in full every month <br><p>
                <input type="radio" name="question1" value="A"> True </radio> 
                <input type="radio" name="question1" value="B"> False </radio> <br> <hr>
               
               
               <p>If I don't make a payment my credit score will be unaffected <br></p>
                <input type="radio" name="question2" value="A"> True </radio> 
                <input type="radio" name="question2" value="B"> False </radio>
    

                <br><br>
                
                <button type="button" class="btn btn-primary btn-xl page-scroll" onclick = "returnScore()">Results</button>

<h4 id="results"> </h4>
                

1 Answer 1

2

Problem is with document.getElementById("results").innerHtml ("Well done! You scored "+ getScore() +"/"+ tot); innerHTML is not a function, you have to assign value to in.

It should be document.getElementById("results").innerHTML = "Well done! You scored "+ getScore() +"/"+ tot;

Sign up to request clarification or add additional context in comments.

2 Comments

Isn't bringing up anything still it has stopped trying to print the page though.
Demo : jsfiddle.net/m563ev87 , make sure there are no typos, its innerHTML not innerHtml , notice the capital HTML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.