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 trialVic Mercier
3,276 PointsI don't understand this excercise.
I don't understand this excercise.
const list = document.getElementsByTagName('ul')[0];
list.addEventListener('click', function(event) {
if (event.target.tagName == 'BUTTON') {
}
});
<!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>
2 Answers
Steven Parker
231,269 PointsTry breaking it down into steps.
The instructions say, "When any one of the buttons is clicked, a class of highlight should be added to the paragraph element immediately preceding that button...". Let's break that down a bit:
- the provided code has already filtered out only
BUTTON
click events - using DOM traversal, select the element previous to the button
- give that element a class name of "highlight"
That may sound like a lot, but it can be accomplished with a single line of code.
Alexander Acker
18,123 PointsThe purpose of this exercise is to help understand the capabilities and methods that JavaScript has in accessing and changing other elements around the DOM during a particular web event. In this particular instance, the click of a particular button should enable code to look for that buttons sibling P element and make a change to it. I utilized the previousElementSibling property here to get this done as the P tag is directly before the button. Useful information within the MDN on this one.
Vic Mercier
3,276 PointsVic Mercier
3,276 PointsThank you so much ! I don't know how you do to break down a problem that seems hard but it isn't!If I am annoying with my questions, it is because my mother tongue is french and I am 11 years old.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsSo you're getting started early learning coding — good for you! Breaking larger problems into smaller steps will become easier with practice. Happy coding!