0

I am creating a quiz and can't figure out why one of my submit buttons won't work.

Here is how the quiz works:

On page load, the questions appear, with possible answers. When the user selects an answer and clicks "Submit", he or she gets feedback on if the answer was correct. A new button, named "next" is presented at the bottom of the page. When it is clicked, the next question in my array should display.

Here is the problem:

The "next" button isn't doing anything when clicked. I set up an alert to test the "next" button, but it's not showing.

The on click function is set up exactly like the previous one, which is working. I'm stumped.

Here is my JSFiddle: http://jsfiddle.net/amykirst/3eubj/2/.

$(document).ready(function() {

  showQuestion();

  // When user clicks submit, grade the question
  $("input[name='submit']").click(function() {
  //alert("line 118: I clicked on submit");
  gradeQuestion();
  });

  // When user clicks "Next Question", show next question
  $("input[name='next']").click(function() {
    alert("line 124: I clicked on next");
    // update currentQuestion to next question
    currentQuestion++;
    //show question
    showQuestion();
  });

  // Prevent form from refreshing page
  $("form").submit(function(e) {
    e.preventDefault();
  });

}); // end document ready

2 Answers 2

1

Depending on wether or not the next button is part of the DOM at the moment you append the event handler you may want to delegate the click event from its parent to the button. This way the event will be handled even on new elements added after the handler has been registered.

$( '.container' ).on( 'click', 'input[name="next"]', function ( e ) {
    alert( 'Load next question now!' );
} );

Also see the documentation for the deprecated .delegate() to which this functionality originates.

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

Comments

0

this line :

$("input[name='next']").click(function() {
alert("line 124: I clicked on next");
// update currentQuestion to next question
currentQuestion++;
//show question
showQuestion();

});

change it to :

 $(document).on("click","input[name='next']",function() {
alert("line 124: I clicked on next");
// update currentQuestion to next question
currentQuestion++;
//show question
showQuestion();

});

apparently you need to trigger that $(document) because that button is dynamically generated.

Working Fiddle

Comments

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.