Abdul M. Diaz Abdul M. Diaz - 4 months ago 7
HTML Question

Javascript function keeps adding text

I made a js quiz. Whenever the "next" button is pressed, the text that reads the question number keeps adding text that display the question number instead of replacing it. To help you understand what I mean, this is the link.

This is the code that runs the function for question number:

var header = $('<p>Question ' + (index + 1) + ' of ' + questions.length + '</p>');
$('#questionnum').append(header);

Answer

Your code should actually use .html() instead of append. Appending means adding at the end while the .html overwrites the innerHTML of the element.

var header = $('<p>Question ' + (index + 1) + ' of ' + questions.length + '</p>');
$('#questionnum').append(header);
Comments