I want to create a counter for my to-do list. It's showing the count of all items. But it does not update when I add an item. It stays at 3.
It's my Javascript code;
var count = document.getElementById("list").childElementCount;
document.getElementById("title").innerHTML = ("You have " + count + " to-dos.");
// add item
function todoList() {
var item = document.getElementById("todoInput").value
var text = document.createTextNode(item)
var newItem = document.createElement("li")
var check = document.createElement("span")
check.className = "circle"
newItem.appendChild(check)
newItem.appendChild(text)
document.getElementById("list").appendChild(newItem)
// clear input value
document.getElementById("todoInput").value = "";
}
document.getElementById("add").addEventListener('click', todoList, null);
It's my Html code;
<div class="progress">
<div id="title"></div>
</div>
<ul id="list">
<li><span class="circle"></span>Buid a task app</li>
<li><span class="circle"></span>Do exercise</li>
<li><span class="circle"></span>Buy headphones</li>
</ul>