JavaScript JavaScript and the DOM Responding to User Interaction Event Delegation

Zachary Luke
Zachary Luke
8,046 Points

This event listener works in preview, but still comes out wrong?

In this question, the objective is:

"Currently, the event listener applies a yellow background color to the section element and its child elements when clicked. Add a condition that changes the background of the <input> elements only."

When I check the preview window, the code works just fine. But when I click "Check Work", it still comes back incorrect, stating:

"Make sure you are filtering out all but the "INPUT" tags."

What am I missing here?

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

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

section.addEventListener('click', (e) => {
    if (event.target.tagName == 'INPUT') {
  event.target.style.backgroundColor = 'blue';
    }
    });
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>

2 Answers

Daniel Baker
Daniel Baker
14,651 Points

"Add a condition that changes the background of the <input> elements only."

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

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

It is asking you to add the conditional to the current event. Change the current even.

Michal Czopek
Michal Czopek
13,911 Points

I am not sure but You have used e and event. I think they have to be the same. Regards Mike

Brett Bodofsky
Brett Bodofsky
2,100 Points

Can anybody confirm this? I used Daniels code and it worked fine. But didn't notice a difference when it came to the preview. Wish the preview didn't go away so fast after getting something correct. It should be the users choice whether to continue or not after getting an answer correct.

Daniel Baker
Daniel Baker
14,651 Points

Brett, I saw the same thing, it was actually your code I was using. I would have change it to e.target.tagName myself except it worked with out changing it. So, I wasn't going to correct you.