1

I have a problem with array in Javascript. I have a button that pushes an item to an array. After I click the button, the for loop which is intended to display a list of items in a div isn't executed.

Below is the code:

HTML:

<input type="text" id="tb">
<button id="btn">Submit</button>
<div id="list"></div>

Javascript:

var list = [];

btn.onclick = function(){
    list.push(document.getElementById("tb").value);
}

for (var i in list){
    document.getElementById("list").innerHTML = "<p>"+list[i]+"</p>"
}

Are there any solutions for this such that after I click the button, the div updates the list of items, much like a to-do list?

4 Answers 4

4

You can just append the html to the list when clicking the button - see demo below:

btn.onclick = function() {
  document.getElementById("list").innerHTML +=
    "<p>" + document.getElementById("tb").value + "</p>"
}
<input type="text" id="tb">
<button id="btn">Submit</button>
<div id="list"></div>

If you want to use the array as a data store as you say in the comments to this answer, you may do something like given in the snippet below:

var list = [];

btn.onclick = function() {
  // add to the list
  list.push(document.getElementById("tb").value);
  // TODO: save to local storage if needed
  // reset the list
  document.getElementById("list").innerHTML = '';
  // display the list
  for (var i in list) {
    document.getElementById("list").innerHTML += "<p>" + list[i] + "</p>";
  }
}
<input type="text" id="tb">
<button id="btn">Submit</button>
<div id="list"></div>

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

5 Comments

Thanks for your answers. May I ask why my for loop above isn't executed?
How about if I want to use array instead of appending items to a div?
well, you can add to a list and append in the btn.onclick and keep the data in the array if that is intended...
the for loop you have written in the question is outside the click listener and you should use +=
I want the list of items displayed from array for future use (local storage).
1

i'd do:

var list = [];

btn.onclick = function(){
    list.push(document.getElementById("tb").value);
    for (var i in list){
        document.getElementById("list").innerHTML = "<p>"+list[i]+"</p>"
    }
}

As you can see the for loop must be triggered in its execution so i moved it inside the function

4 Comments

Thanks for your answers.
Your code doesn't create a list of items. It only displays the lat item (index) from the for loop.
@billyhalim25 my code was just to demonstrate that to trigger the execution of the for loop it has to be included in the function that is called on click. OP is asking about why it isn't executed. I agree with your feedback about what the code does :)
Okay. I'm sorry for that.
1

Just append a new <p> element to the list instead of using innerHTML. Also, you should bind event listeners using addEventListener:

var list = document.getElementById('list');
var input = document.getElementById('tb');

document.getElementById('btn').addEventListener('click', function () {
  var p = document.createElement('p');
  p.textContent = input.value;
  list.appendChild(p);
});
<input type="text" id="tb">
<button id="btn">Submit</button>
<div id="list"></div>

Comments

1

var list = [];
var btn = document.getElementById("btn");

btn.onclick = function(){
    var tb = document.getElementById("tb").value;
    //list.push(tb);
    document.getElementById("list").innerHTML += "<p>"+tb+"</p>"
}
<input type="text" id="tb">
<button id="btn">Submit</button>
<div id="list"></div>

2 Comments

Thanks for your answers.
But I want an answer using array instead of appending.

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.