Search code examples
javascriptlistappendappendchild

my list function doesn't work in javascript


so i wrote this code that would create a list and then append an input to it on click. really simple. but the problem is it doesn't work and i have no idea why here is the code:

function pushing() {
  var li = document.createElement("li");
  var inputValue = document.getElementById("inp").value;
  var pushchild = document.createTextNode(inputValue);
  li.appendChild(pushchild);
}

sub.addEventListener("click", pushing);

the id inp is an input id. thank you


Solution

  • Add this to the last line of your function. Append your newly created li element to the ul.

    document.querySelectorAll(‘ul’).appendChild(newCreatedLi);