My event bound to close
's class is working on the li
that are included on my HTML, but it is not working on the li
's I create with my addItem
function. I can't figure out why. Though, I add the span which contains the class & the textNode to each of my new item.
function addCross() {
let myNodeList = document.getElementsByTagName('li');
for (let i = 0; i < myNodeList.length; i++) {
let span = document.createElement('span');
let cross = document.createTextNode('\u00D7');
span.className = 'close'
span.appendChild(cross);
if (myNodeList[i])
myNodeList[i].appendChild(span);
}
}
addCross();
//---------------------------------------------
let close = document.getElementsByClassName('close');
for (let i = 0; i < close.length; i++) {
close[i].addEventListener('click', function() {
let cross = close[i].parentElement;
cross.style.display = 'none';
});
}
//-----------------------------------------------
function addItem() {
let ul = document.getElementById('list');
let inputText = document.getElementById('inputText').value;
let li = document.createElement('li');
let textNode = document.createTextNode(inputText)
let span = document.createElement('span');
let cross = document.createTextNode('\u00D7');
span.className = 'close'
span.appendChild(cross);
li.appendChild(textNode);
li.appendChild(span);
if (inputText === '') {
alert('enter some task, please')
} else {
ul.appendChild(li);
}
}
```