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 trialSarah Yasi
7,860 PointsNot sure why this code isn't working.
I can't seem to figure out why the input element isn't being targeted properly.
let section = document.getElementsByTagName('section')[0];
section.addEventListener('click', (e) => {
if (event.target.targetName = 'INPUT') {
e.target.style.backgroundColor = 'rgb(255, 255, 0)';
}
});
<!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
KRIS NIKOLAISEN
54,972 PointsSome hints:
- The property you are looking for is
tagName
- You are using an assignment operator in your if condition. To make a comparison use
==
.
Juan Lopez
Treehouse Project ReviewerHey Sarah good job on writing your if
statement. So there are a couple of minor details that need changing to pass the challenge.
if (event.target.targetName = 'INPUT') {
So starting off it seems as though there might be a small mistake as to the property you were trying to access from the event.target
looks as though you might have meant tagName
instead of targetName
.
If you do a console.dir(event.target)
and open your Chrome Dev Tools you'll be able to see what properties are accessible from event.target
.
Secondly, keep in mind the =
will try and assign a value rather than check a value. With that being said you'll want to use the ===
to check if the event.target.tagName
is equal to INPUT
like so
section.addEventListener('click', (e) => {
if (e.target.tagName === 'INPUT') {
e.target.style.backgroundColor = 'rgb(255, 255, 0)';
}
});