Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Responding to User Interaction The Event Object

Joseph Anson
Joseph Anson
14,445 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
210,070 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.