1

I have appended 2 buttons for each li element in my app:

  todoLi.appendChild(this.createToggleButton());
  todoLi.appendChild(this.createDeleteButton());

Later, I added event listeners to trigger when li is moused over and out

todosUl.addEventListener('mouseover', function(event) {
  let elementMousedOver = event.target;
  if(elementMousedOver.className == 'todoLi') {
    elementMousedOver.lastElementChild.style.display = 'inline';
  }
});
todosUl.addEventListener('mouseout', function(event) {
  let elementMousedOver = event.target;
  if(elementMousedOver.className == 'todoLi') {
    elementMousedOver.lastElementChild.style.display = 'none';
  }
});

Everything works fine for li element, but when I mouse over the buttons, that are children of the li element, event listeners stop working for some reason if the bottons were not children of li element after all.

How can I make appended children to also trigger its parent's event listener?

Here is my app on github: https://mikestepanov.github.io/

repo with files: https://github.com/mikestepanov/mikestepanov.github.io

  • @JosanIracheta I must say that HTML is implicitly included in the question (`Ul`, `Li`, `Button`s). – ibrahim mahrir Nov 25 '17 at 23:59
  • @JosanIracheta here is my HTML : https://github.com/mikestepanov/mikestepanov.github.io/blob/master/index.html –  Nov 25 '17 at 23:59
  • @Mikhail unless the `button` elements also contain the `className` of "todoLi", they will be disregarded – JJJ Nov 26 '17 at 00:01
  • @charlietfl if I mouse over li or any child of li, it will trigger li element if I put its children through regular html. However, it does not work for me if I append children in javascript –  Nov 26 '17 at 00:03
  • @Mikhail if you look at this fiddle, you will notice that the `mouseover` event is being triggered: https://jsfiddle.net/o97jqz50/. I am pretty sure that your `button` elements do not have the same `className` as your `li` elements and therefore your conditional statement is not being executed – JJJ Nov 26 '17 at 00:05
  • I think this is related to **event bubbling/propagation**. – ibrahim mahrir Nov 26 '17 at 00:07
  • Possible duplicate of [What is event bubbling and capturing?](https://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing) – ibrahim mahrir Nov 26 '17 at 00:11