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?