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

In the listener that has been added to the section element, ensure that the text input elements are the only children th

Anybody got the answer?

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>
              <div>
                <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>
              </div>
            </ul>
            <button>Save</button>
        </section>
        <script src="app.js"></script>
    </body>
</html>
app.js
let section = document.getElementsByTagName('section')[0];

section.addEventListener('click', (div) => {
  textInput.target.style.backgroundColor = 'rgb(255, 255, 0)';
});

3 Answers

since it is named div in your event handler. You should use the target.tagName property for the div object.

let section = document.getElementsByTagName('section')[0];

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

Steven Parker
Steven Parker
229,787 Points

You won't want to change the event argument passed to the handler.

You don't have anything named textInput defined anyway. But if you put the code provided back to the way it was (or restart the challenge), you can accomplish the task by adding some new code to perform a test to limit when the color assignment can be done.

Hint :point_right: HTML elements (including an event target) all have a tagName property that could be handy here.

Steven Parker what does "ensure that the text input elements are the only children" means, i am lost which elements should i select.

Steven Parker
Steven Parker
229,787 Points

This is where that tagName property I hinted about before could be useful. If you were to compare the value stored there with the string "INPUT", for example, any time that comparison is true you would know current even target was an input element. This would be a way to distinguish those elements from the other children of the selection.

Steven Parker I was doing it using h1, p and all where text. So you think "Input" is the answer?

Steven Parker
Steven Parker
229,787 Points

I'm not sure what you mean by "all where text". This is apparently something other than what you show above. Could you show the code as you have it now?