My JS code:- it's been just 1 month learning JS, so pls help me out.
Here inside addItems function I wrote a for-loop to check duplicate list with the help of textContent of list and input value of input text box. But it's not working.
var btn = document.querySelector("button");
var element = document.querySelector("#todo-list");
var children = element.childElementCount;
var li = document.querySelectorAll("li");
var input = document.querySelector("input");
btn.addEventListener("click", addItems);
document.addEventListener("keydown", function(event){
if(event.key == "Enter")
{
addItems();
}
});
function addItems()
{
if(input.value == "")
{
alert("Empty List cannot be created");
}
else{
var list = document.createElement("li");
var textNode = document.createTextNode(input.value);
children++;
list.appendChild(textNode);
element.appendChild(list);
console.log(children);
//This loop is not working properly after adding new list
for(var i=0; i<children; i++)
{
if(li[i].textContent == input.value){
alert("Duplicate List");
}
}
}
}
