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 trialNebojsa Stevanovic
Full Stack JavaScript Techdegree Student 9,620 Pointscan i get help here please
Hello.. i don't understand this task.. when i click button, i have to add a class that has a name 'highlight' to p element ..right ?
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>
2 Answers
Ewerton Luna
Full Stack JavaScript Techdegree Graduate 24,031 PointsDon't worry. Some times things get a bit confusing. But once you get it, it will stick to your brain. This is a process and we've all been there. Maybe the only thing you're missing is how the DOM tree works. Child elements are nested inside the parents tags. The parent elements have a higher hierarchy. Sibling elements are not one inside of the other. They are on the same hierarchy level, hence they "stand" side by side.
Take a look at the ul children:
<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>
Each li element is parent of both a p element and a button element. You can see both p and button tags are inside li and stand side by side (meaning they are not one inside of the other).
So, if you use the even.target property to identify which button was clicked, you can get a reference to the p element doing
var p = e.target.previousElementSibling
You can do this and be sure it will give you a p element because you already conditioned the handler to run the code if the event target is a button element.
So, your solution would look something like this (SPOILERS! Don't look at the snippet below if you just wanna keep trying yourself):
var list = document.getElementsByTagName('ul')[0];
list.addEventListener('click', function(e) {
if (e.target.tagName == 'BUTTON') {
var p = e.target.previousElementSibling;
p.className = "highlight";
}
});
Ewerton Luna
Full Stack JavaScript Techdegree Graduate 24,031 PointsYes. But it can't be any p element. It has to be the imediate previous element sibling. The one right next to it.
Remember to use the event object and the previousElementSibling property. Take it from there! If you're still having some trouble, come here again and I'll show you a solution!
Nebojsa Stevanovic
Full Stack JavaScript Techdegree Student 9,620 Pointsimediate previous sibiling woud be li element ??? i am so confused, i think i will have to rewatch all videos again to solve this..
Nebojsa Stevanovic
Full Stack JavaScript Techdegree Student 9,620 PointsNebojsa Stevanovic
Full Stack JavaScript Techdegree Student 9,620 Pointswow.. i've been reading this comment that you wrote for 10 min already.. you are right, i do have a problem with DOM tree how it works.. but you really helped me with this answer, is so thoroughly explained. thank you !
Ewerton Luna
Full Stack JavaScript Techdegree Graduate 24,031 PointsEwerton Luna
Full Stack JavaScript Techdegree Graduate 24,031 Pointshaha No problem! I'm glad I could help. Keep it up!