Garmaine Staff asked 1 year ago
var button = document.querySelector("button");
var input = document.querySelector("input");
var ul = document.querySelector("ul");
var checkInputlength = input.value.length>0;

function createList() {
  if (checkInputlength) {
    var node = document.createTextNode(input.value);
    var li = document.createElement("li");
    li.appendChild(node);
    ul.appendChild(li);
  }
}

button.addEventListener("click", createList);

checkInputlength always returns false for some reason. But, when I put the condition inside the if statementif (input.value.length>0), it works as expected. Can anyone tell me why?