0

So i am making a todo list. The event does not detect removal of li elements, but only when i add li elements.

i couldn't find any good reference to event types...

What type of evenlistener should i use for detecting both removing and adding

var ul_completed = document.createElement('ul');
ul_completed.id = res_data.list_name + 'completed';
ul_completed.classList = "group";

var li_completed = document.createElement('li');
li_completed.id = res_data.list_name + 'completed_li';

var completed_container = document.createElement('div');
li_completed.classList = 'li_completed';

var li_completed_name = document.createElement('p');
li_completed_name.innerHTML = 'No completed tasks';

ul_completed.addEventListener('change', function(e) {
    console.log('something changed');
    if(this.childElementCount == 1){
        li_completed_name.innerHTML = 'No completed tasks...yet';
    } else {
        li_completed_name.innerHTML = 'Completed tasks';
    }

});

the elements are appended after the eventlistener...

//APPENDING TO COMPLETED

completed_container.appendChild(li_completed_name);
li_completed.appendChild(completed_container);
ul_completed.appendChild(li_completed);

I CAN ONLY USE PLAIN JAVASCRIPT.

keep in mind, i do not want to delete the items. i just want the first li in the list to display a different text when there are more than one li in the ul.

RoyalKode
  • 19
  • 3
  • @DavidThomas, I agree that using only plain javascript is a sound requirement. But forbidding html? It is a red flag that this could be a homework problem. Professors sometimes do that in order to prevent students from solving HW problems by using html/css hacks. Writing such a simple program without using any html is odd in my opinion. – jrook Nov 28 '17 at 04:05
  • @jrook The thing is...The data that will be contained/inserted/removed in the list is from a database. Therefore no hardcoding elements in HTML. Never said u couldnt use HTML....(or css for that matter), Html code is not what i am asking for. This is an Express app with nodejs...not that it matters. – RoyalKode Nov 28 '17 at 12:02
  • @RoyalKode I apologize for making the unwarranted assumption. – jrook Nov 28 '17 at 18:59