-2

I don’t think the code in the $(document).ready(); is executing; neither of the elements show up. I've declared the button and text earlier on and know it works from previous test. I think I have sort of an idea -- the $(document).ready(); executes at pageload, not after the main();. But, I don't understand why the code inside the submit click handler wouldn’t execute and show the necessary elements, anyway.

EDIT: Console output said that the number variable was undefined.

question is a bunch of JSON, which is also known working...

function randomIntFromInterval(min,max)
{
    return Math.floor(Math.random()*(max-min+1)+min);
}
function main()
{
var number = randomIntFromInterval(1,4);
$("#main").html('<br>' + questions[number]["Question"] + '<br>' + '<form action="" id="questionblock">' + '<input type="radio" name="choice" value="Choice1">' + questions[number]["Choice1"] + '<br>' + '<input type="radio" name="choice" value="Choice2">' + questions[number]["Choice2"] + '<br>' + '<input type="radio" name="choice" value="Choice3">' + questions[number]["Choice3"] + '<br>' + '<input type="radio" name="choice" value="Choice4">' + questions[number]["Choice4"] + '<br>' + '</form>');
}
main();
$(document).ready(function(){
  $("#reset").hide();
  $("#submit").click(function(){
    var uc = $('input[name=choice]:checked', '#questionblock').val();
    var cc = questions[number]["Correct"];
    if (uc == "undefined") {
        document.write("Please choose an answer");
    } else {
      if (uc == cc) {
        $("#correct").html("Correct!");
        $("#reset").show();
      } else {
        $("#correct").html("incorrect");
        $("#reset").show();
      };
    };
  });
});
evamvid
  • 831
  • 6
  • 18
  • 40

1 Answers1

2

Set your number as a global variable, then assign the global number var in main

<script>
var number;

function randomIntFromInterval(min,max)
{
    return Math.floor(Math.random()*(max-min+1)+min);
}
function main()
{
number = randomIntFromInterval(1,4);
$("#main").html('<br>' + questions[number]["Question"] + '<br>' + '<form action="" id="questionblock">' + '<input type="radio" name="choice" value="Choice1">' + questions[number]["Choice1"] + '<br>' + '<input type="radio" name="choice" value="Choice2">' + questions[number]["Choice2"] + '<br>' + '<input type="radio" name="choice" value="Choice3">' + questions[number]["Choice3"] + '<br>' + '<input type="radio" name="choice" value="Choice4">' + questions[number]["Choice4"] + '<br>' + '</form>');
}
main();
$(document).ready(function(){
  $("#reset").hide();
  $("#submit").click(function(){
    var uc = $('input[name=choice]:checked', '#questionblock').val();
    var cc = questions[number]["Correct"];
    if (uc == "undefined") {
        document.write("Please choose an answer");
    } else {
      if (uc == cc) {
        $("#correct").html("Correct!");
        $("#reset").show();
      } else {
        $("#correct").html("incorrect");
        $("#reset").show();
      };
    };
  });
});
Gary Schreiner
  • 902
  • 5
  • 14