JavaScript JavaScript and the DOM Responding to User Interaction The Event Object

Joseph Anson
Joseph Anson
14,442 Points

Is it okay to choose !== ul or would e.target.tagName === 'LI' be the best option?

I paused the video to try to figure out how I would select the li inside the ul to make them uppercase/lowercase without using the for loop. It took me quite a long time but felt great having managed to do it! Then I watched the video and he did it differently, same result but different.

Here is my code:

const ul = document.querySelector('ul');


  ul.addEventListener('mouseover', (e) => {

    if (e.target !== ul) {
      e.target.textContent = e.target.textContent.toUpperCase();
    }
  });

  ul.addEventListener('mouseout', (e) => {
     if (e.target !== ul) {
      e.target.textContent = e.target.textContent.toLowerCase();
    }
  });

My question is, does it matter that I've used !== ul or should I be using the .tagName instead (i didn't even think about that unfortunately).

Thank you.

1 Answer

Steven Parker
Steven Parker
203,212 Points

The inequality test would be true for any other element, but testing the tag name guarantees that the rest of the code will only be applied to list items.