1

how to display element one by one on click using only Javascript, in my example when I click all elements show at once, but i need only one click - one element. I appreciate if you show the simplest way to do if in order to i can understand how it works

$(function() {
    var cars = ["audi", "bmw", "volvo"];
    var x = "";
    var i;
    for (i = 0; i <cars.length; i++) {
        x += cars[i] + "<br>";
    }

    document.getElementById("btn").onclick = function() {
        document.getElementById("text").innerHTML = x;
    }
});
4
  • "using only Javascript"? Do you mean without jQuery? So why are you using $? Commented Jul 14, 2016 at 11:46
  • without Jquery during finding elements in array Commented Jul 14, 2016 at 11:47
  • can you add example into jsfiddle.net Commented Jul 14, 2016 at 11:47
  • Do you need something like javascript yielddeveloper.mozilla.org/en/docs/Web/JavaScript/Reference/… Commented Jul 14, 2016 at 11:47

3 Answers 3

3

You may update your code as follows. At the very beginnig, your are initializing x with empty string. Then for each click on button, append an element from array with new line tag.

var cars = ["audi", "bmw", "volvo"];
var x = "";
var i = 0;

document.getElementById("btn").onclick = function() {
  if( i < cars.length) {
    x += cars[i++] + "<br>";
  }
  document.getElementById("text").innerHTML = x;
}
<p id="text"></p>
<button id="btn">Result</button>

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

1 Comment

thanks a lot, you really helped, i read that immediate function have to deal with such tasks well, but it didn't work when i use it, but your example really nice
0
<html> 
<script>
        var cars = ["audi", "bmw", "volvo"];
        var x = "";
        var count = 0;
          function appendArray(){

        if(count<cars.length){
        x += cars[count]+ "<br>";
            document.getElementById("appendText").innerHTML = x;
            count++;
            }else{
        count = 0;
        document.getElementById("appendText").innerHTML = "";
        }

     }
    </script>


    <p id="appendText"></p>
    <button onclick="appendArray()">Submit</button>
    </html>

Comments

0

Here is one solution in vanilla javascript:

var button = document.getElementsByTagName('button')[0];
var i = 0;

function addCar(i) {
    var cars = ['audi', 'bmw', 'volvo'];
    var paragraph = document.getElementsByTagName('p')[0];

    if (i < cars.length) {
        var newLine = document.createElement('br');
        var newCar = document.createTextNode(cars[i]);
        paragraph.appendChild(newLine);
        paragraph.appendChild(newCar);
    }
}

    
button.addEventListener('click',function(){addCar(i); i++;},false);
<p></p>
<button>Click for a New Car</button>

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.