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 trialPiotr Manczak
Front End Web Development Techdegree Graduate 29,363 PointsI am not sure if I know what they want from me. Do they want me to change class name of the <p> to highlight?
If yes, which method of targeting a <button> element would be best?
var list = document.getElementsByTagName('ul')[0];
var button = document.getElementsByTagName('button');
var p = button.previousElementSibiling;
list.addEventListener('click', function(e) {
if (e.target.tagName == 'BUTTON') {
p.className = 'highlight';
}
});
<!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>Things to Learn</p>
<ul>
<li><p>Element Selection</p><button>Highlight</button></li>
<li><p>Events</p><button>Highlight</button></li>
<li><p>Event Listening</p><button>Highlight</button></li>
<li><p>DOM Traversal</p><button>Highlight</button></li>
</ul>
</section>
<script src="app.js"></script>
</body>
</html>
4 Answers
Piotr Manczak
Front End Web Development Techdegree Graduate 29,363 Pointsok, I got it. I made it too complicated. This would do, I guess:
var p = e.target.previousElementSibling;
p.className = 'highlight';
Louise St. Germain
19,424 PointsHi Piotr,
Yes, they are looking for you to change the class name of the p to "highlight", but only for the p that is right before the button that got clicked.
You have the right idea to use previousElementSibling to find the p, but you will need to do that inside the event listener. If you do it before (as in your code above), it selects a p before you've even clicked a button, which is not what we want.
So, your code should be something like:
var list = document.getElementsByTagName('ul')[0];
list.addEventListener('click', function(e) {
if (e.target.tagName == 'BUTTON') {
// TODO: Find e.target's previousElementSibling (which should be a p)
// TODO: Set that element's class name to "highlight".
}
});
I hope this helps to clarify!
Piotr Manczak
Front End Web Development Techdegree Graduate 29,363 Pointsassuming I selected correctly <p>, is the command below a correct way to change paragraph's class?
p.className = 'highlight';
Piotr Manczak
Front End Web Development Techdegree Graduate 29,363 PointsWhat do you think about this:
var list = document.getElementsByTagName('ul')[0];
list.addEventListener('click', function(e) {
if (e.target.tagName == 'BUTTON') {
var li = e.target.parentNode;
var button = document.getElementsByTagName('button');
var p = button.previousElementSibiling;
p.className = 'highlight';
}
});
MOD: Just reformatted so the code is easier to read!
Piotr Manczak
Front End Web Development Techdegree Graduate 29,363 PointsYou're right. This is even better.
Louise St. Germain
19,424 PointsLouise St. Germain
19,424 PointsYes, the simple solution works! :-)
In fact, you could simplify two lines into one, like this: