0

I made the remove item button with the js code while doing the to-do list project.

removing items which are earlier made in Code editor works well. *so in browser preview, I typed and added "new To-do" item to list.* but removing "new To-do" item doesn't work. even chrome Devtool shows no error. new item's tag and className is all correct.

p.s : don't need to worry about 'X' or X icon. I skipped for uploading.

[HTML]

<ul id="listUl">
    <li>Meet George <span class="remove"></span></li>
    <li>Pay Bills <span class="remove"></span></li>
    <li>new To-do <span class="remove"></span></li>
</ul>

[JS] - removing part.

var remove = document.getElementsByClassName('remove');

for (var i = 0; i < remove.length; i++) {
    remove[i].onclick = function() {
        var target = document.getElementById('listUl');
        target.removeChild(this.parentElement);
    }
}
MRM
  • 11
  • 3

6 Answers6

3

That's because every new item you add does not have any listener on them (remove[i].onclick = function(){...}). So when you click on them, nothing happens. You need to add the onClick listener to every new items, like you are doing for the initial ones.

Nicolas
  • 7,276
  • 3
  • 17
  • 40
1

I rewrote your code:

There is only one (delegated) event assigned now to the <ul> parent element, it will trigger only, when the click happens on an element with class="remove". In that case it is the <span> element which I also made a bit more visible by adding an "X" inside. Right, when the action occurs I have to go up two levels until I reach the parent of the actual element I want to remove: the <li> element.

var i=3,UL=document.querySelector('#listUl');
UL.addEventListener('click',function(ev){
  //   ev.target                       - this element was clicked on
  if (ev.target.classList.contains('remove'))
    // ev.target.parentNode            - this must be the <li> element
    // ev.target.parentNode.parentNode - and this is the <ul> element 
    ev.target.parentNode.parentNode.removeChild(ev.target.parentNode);
})
document.querySelector('#addTask').addEventListener('click',
function(){UL.innerHTML+='<li>and task no. '+(++i)+'... <span class="remove">X</span></li>'})
<button id="addTask">add another task</button>
<ul id="listUl">
<li>Meet George <span class="remove">X</span></li>
<li>Pay Bills <span class="remove">X</span></li>
<li>new To-do <span class="remove">X</span></li>
</ul>

The "add" button demonstrates how newly created<li>s also respond to clicks on the "remove"-span.

Carsten Massmann
  • 16,701
  • 2
  • 16
  • 39
1

Add the onClick listener to ul tag. Check

    if(e.target && e.target.nodeName == "LI") {
            //Your code here
    }
0

It looks like that the JS code only adds delete function to these todos that already exists when the page loads. Newly added todos will not have onclick callback function attached if you only execute your js once when the pages loads.

The workaround here is to attach the callback function every time you create a TODO element.

Jeff
  • 73
  • 8
0

Your newly (dynamically) added DOM elements don't have event listeners attached to them, because on page load, you only iterated (looped) only currently available elements, so You may try the following approach (Read About Event Delegation):

document.getElementById('listUl').addEventListener('click', function(e) {
    if (e.target.nodeName == 'LI') {
     e.target.parentNode.removeChild(e.target);
    }
});
<ul id="listUl">
  <li>Meet George <span class="remove">X</span></li>
  <li>Pay Bills <span class="remove">X</span></li>
  <li>new To-do <span class="remove">X</span></li>
</ul>
The Alpha
  • 131,979
  • 25
  • 271
  • 286
0

if you have jquery added to your page, you can do it like

$('#listUl').on('click', 'span.remove', function() {
  $(this).parent().remove();
});

$('.add').click(function() {
  $("#listUl").append("<li>Whatever Content <span class='remove'>x</span</li>");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="listUl">
  <li>Meet George <span class="remove">x</span></li>
  <li>Pay Bills <span class="remove">x</span></li>
  <li>new To-do <span class="remove">x</span></li>
</ul>
<butoon class="add">Add</butoon>
Ankit
  • 660
  • 4
  • 17