Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

if (e.target.tagName == 'LI') .... so how come this handler isn't filtering out all but the list iteams?

nothing more

index.html
<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript and the DOM</title>
    </head>
    <link rel="stylesheet" href="style.css" />
    <body>
        <section>
            <h1>Making a Webpage Interactive</h1>
            <p>JavaScript is an exciting language that you can use to power web servers, create desktop programs, and even control robots. But JavaScript got its start in the browser way back in 1995.</p>
            <hr>
            <p>Things to Learn</p>
            <ul>
                <li>Item One: <input type="text"></li>
                <li>Item Two: <input type="text"></li>
                <li>Item Three: <input type="text"></li>
                <li>Item Four: <input type="text"></li>
            </ul>
            <button>Save</button>
        </section>
        <script src="app.js"></script>
    </body>
</html>
app.js
let section = document.getElementsByTagName('section')[0];

section.addEventListener('click', (e) => {
  if (e.target.tagName == 'LI') {
  e.target.style.backgroundColor = 'rgb(255, 255, 0)';
  }
});

4 Answers

Steven Parker
Steven Parker
244,128 Points

What makes you think it's not filtering?

The filtering seems to work as expected. When I click on the list items, the background color changes, and when I click any other section elements it does not.

The problem seems to be that the list items are not what the challenge asked for. The challenge said to limit the event handling to the INPUT elements. So the filtering is indeed working, just on the wrong things.

I am not saying it lol "Bummer! Make sure you are filtering out the right elements." tree-house is?

Steven Parker
Steven Parker
244,128 Points

I didn't see the challenge link before, but now that it's there I can see this is a case of something that "works" but performs a different task than the one requested.

OH limit the INPUT..... lol i got you

Hi Logan,

The challenge wants the inputs to trigger the background change. Not the list items.

I'm a bit confused as to why INPUT needs to be in all caps.

It returns every thing in caps. INPUT is not equal to Input. if its <p> you put P, if its <li> you put LI, ect...

if (e.target.tagName == 'LI') /////// e.target.tagName returned targeted tagName in caps

Hey guys, just a quick question! Why it needs to be in all caps the tag name? which function or i dont know what transforms the tag name in caps?