jquery - How to Learn JavaScript Correctly Quiz Issue -
i'm working through how learn javascript correctly. halfway through has create quiz application. i'm able display first question, clicking next button changes second question. problem when click button third time, screen clears , third question never appears.
i'm sure i'm missing easy. idea i'm going wrong?
app.js:
var allquestions = [ {question: "who prime minister of united kingdom?", choices: ["david cameron", "gordon brown", "winston churchill", "tony blair"], correctanswer:0}, {question: "who president of united states?", choices: ["george bush", "barack obama", "hilary clinton"], correctanswer:1}, {question: "what best state?", choices: ["iowa", "wisconsin", "colorado", "north carolina"], correctanswer:1} ]; var score = 0; var = 0; $(document).ready(function() { $('#next').addclass('hidden'); nextquestion(); }); function nextquestion() { var container = $('#container'); var questionname = allquestions[i].question var answer = allquestions[i].correctanswer; var choice = 0; var question = "<div>" + questionname + "</div>"; container.append(question + "<br>"); var choices = allquestions[i].choices; (var j=0;j<choices.length;j++) { var choice = choices[j]; var radio = "<input type='radio' data-choice='" + j + "' value='" + choice + "' name='" + allquestions[i].question + "'>" + choices[j]; container.append(radio + "<br>"); } $('input:radio').on('click',function() { choice = $(this).data("choice"); $('#next').removeclass('hidden'); }); $('#next').on('click',function() { $('#next').addclass('hidden'); if (choice === answer) { alert("winner!"); } if (i < allquestions.length) { += 1; } container.empty(); nextquestion(); }); }
index.html:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1"> <title>survey</title> <link rel="stylesheet" href="assets/bootstrap.min.css"> <link rel="stylesheet" href="assets/bootstrap.responsive.min.css"> <link rel="stylesheet" href="assets/base.css"> </head> <body> <!-- index.html --> <div id="container"> <script src="js/lib/jquery.min.js"></script> <script src="js/lib/underscore-min.js"></script> <script src="js/lib/bootstrap.min.js"></script> <script src="js/app.js"></script> </div> <input type='submit' value='next' id='next'> </body> </html>
remove following code withing function; you're binding events multiple times. @ time when click #next
once may advance multiple time responds multiple binds.
$('input:radio').on('click',function() { choice = $(this).data("choice"); $('#next').removeclass('hidden'); }); $('#next').on('click',function() { $('#next').addclass('hidden'); if (choice === answer) { alert("winner!"); } if (i < allquestions.length) { += 1; } container.empty(); nextquestion(); });
put code inside dom ready event modification shown:
$(function() { $(document).on('change', 'input:radio', function() { choice = $(this).data("choice"); $('#next').removeclass('hidden'); }); $('#next').on('click',function() { $('#next').addclass('hidden'); if (choice === answer) { alert("winner!"); } if (i < allquestions.length) { += 1; } container.empty(); nextquestion(); }); });
Comments
Post a Comment