Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: How to display an array of paragraph tags one by one on each click?

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?

like image 248
user298519 Avatar asked May 04 '16 16:05

user298519


Video Answer


1 Answers

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++;
  }

});
like image 73
R3tep Avatar answered Oct 08 '22 00:10

R3tep