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 trialcharlie alton
Front End Web Development Techdegree Student 8,879 PointsI cant figure this out.
I dont remember them teaching me this in this course. I need help?
var list = document.getElementsByTagName('ul')[0];
list.addEventListener('click', function(e) {
if (e.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>
1 Answer
Joseph Yhu
PHP Development Techdegree Graduate 48,637 Points- Use the
previousElementSibling
property fore.target
to select the<p>
element that's beside each<button>
element. - Use the
className
property to set the classes of the selected<p>
elements tohighlight
.
Karla Walker
Full Stack JavaScript Techdegree Graduate 15,865 PointsKarla Walker
Full Stack JavaScript Techdegree Graduate 15,865 PointsHi charlie alton ! So the code challenge is asking you to add a class to the
<p>
tag, which is the previous sibling of the button you are clicking. The<button>
element you are clicking and need to reference is equal toe.target
. The challenge shares a link to thepreviousElementSibling
property that you can use to get the<p>
tag, I shared the link below:https://developer.mozilla.org/en-US/docs/Web/API/Element/previousElementSibling
Once you have the
<p>
tag saved, you can useclassList.add()
to add the 'highlight' class to the<p>
tag.Here's a ink to the
classList
documentation in case you need a refresher on that:https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
I hope this helps! :)