How to store a data with javascript using objects -


i wrote code:

$(".awesome").click(function () {         var tostore = $("input[name=name]").val();         if (/^[a-za-z]+ [a-za-z]+$/.test(tostore)) {             $("#contain").children().fadeout(1000);             $("#contain").delay(1000).queue(function () {                 $("#contain").append("<h1>welcome quiz : " + tostore + "</br>" +                     "youll 10 questions answer </br> " +                     "here first one:who prime minister of united kingdom? </h1>");                  var allquestions = [                     {question: "who prime minister of united kingdom?",                         choices: ["david cameron",                             "gordon brown",                             "winston churchill",                             "tony blair"],                         correctanswer: 0}                 ];                 $("#contain").append("<form><input type='radio' name='ans1' value='david cameron'>david cameron</br>" +                     " <input type='radio' name='ans1' value='gordon brown'>gordon brown</br>" +                     "<input type='radio' name = 'ans1' value = 'winston churchill' > winston churchill </br>" +                     "<input type='radio' name='ans1' value='tony blair'>tony blair</br>" +                     "<input type='submit' value='submit' name='submit'></form>");                 $("input[name=submit]").click(function () {                     var correctanswer;                     if ($("input[name=ans1]").val()) {                         var x = "choices";                         allquestions[x] = "david cameron";                         (var x in allquestions) {                             return correctamswer = false;                              if (allquestions[x] === "david cameron") {                                  return correctanswer = true;                             } else {                                 return correctanswer = false;                             }                          }                     }                 });             });         } else {             alert("you must put valid name");         }     }); }); 

now can see have object called "allquestions". have 3 properties: "question" "choices" , "correct answer". afterwards have submit button if click it checks value of input name="ans1",then loop through of choices in object "allquestions"; if answer "david cameron" want store correctanswer 1 instead of 0; else don't want store correctanswer , remains 0 because have more questions afterwards. suggestions how so?

  • the first problem direct event handling not work dynamically added elements if register event handler before elements added. in case, think works because register event handler after elements added. should change to:

    $("#contain").on("click","input[name=submit]", function(){ function})

  • the second problem way access allquestions not correct because it's array. should be: allquestions[0][x];

  • the third problem @benjamin gruenbaum mentioned.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -