0

I want when the user clicks the "next" button to move to the next question. The problem here is when I click the button, it only show the first question. Where is the problem? I know 'return' returns the result to the function, but next time when I press the button, the iterator(i) is not i+1?

var changeQuestion = function() {
    for (var i = 0; i < allQuestions.length; i++) {
        var newNode = document.createTextNode(allQuestions[i].question);
        return question_paragraph.replaceChild(newNode, question_paragraph.firstChild);
    }
};

EventUtil.addHandler(next_button, 'click', changeQuestion);
1
  • can you show us the code for changeQuestion ? Commented Jun 28, 2014 at 8:45

1 Answer 1

1

Try this :

var i = -1;
function changeQuestion() {
    i = (i < allQuestions.length) ? (i+1) : -1;
    var newNode = document.createTextNode(allQuestions[i].question);

    return question_paragraph.replaceChild(newNode, question_paragraph.firstChild);
}
EventUtil.addHandler(next_button, 'click', changeQuestion);
Sign up to request clarification or add additional context in comments.

3 Comments

Is this working because of the execution context(the var i is declared in the global scope)?
That's one of the reasons. The other is that I don't use for I increase i on each request. (also, if the end of the array is reached it sets back to the first element)
it doesn't seem to go back to the first element

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.