5
var sentences = ['sentenceone', 'another sentence', 'another sentence again'];

$(".btn").on('click', function() {

    for(var i=0; i < sentences.length; i++) {
        samplebox.innerHTML += '<p>'+sentences[i]+'</p>';
    }
});

This displays all of them in one click. How do I fix this?

2 Answers 2

6

You can displays the paragraphs one by one like this :

var sentences = ['sentenceone', 'another sentence', 'another sentence again'];
var i = 0;

$(".btn").on('click', function() {

  if (i < sentences.length) {
    samplebox.innerHTML += '<p>' + sentences[i] + '</p>';
    i++;
  }

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

Comments

0
<button class="btn" data-step="1">Click here</button>

var sentences = ['sentenceone', 'another sentence', 'another sentence again'];

$(".btn").on('click', function() {
        var limit = parseInt( $(this).attr("data-step") ) - 1;
        for(var i=0; i < limit; i++) {
            samplebox.innerHTML += '<p>'+sentences[i]+'</p>';
        }
        $(this).attr("data-step") = limit + 2;
    });

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.