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 trial

JavaScript

Javascript and the Dom - Traversing the Dom - Sibling Traversal Challenge

I have run this code a million times and it works, but the challenge is failing....any ideas?

var list = document.getElementsByTagName('ul')[0];

list.addEventListener('click', function(e) { if (e.target.tagName == 'BUTTON') { let li = event.target.parentNode; let nextLi = li.previousElementSibling; let ul = li.parentNode; let lastLi = ul.lastElementChild;

if(nextLi){
let p = nextLi.firstElementChild;

p.className = "highlight";
}
if(!nextLi){
 let p = lastLi.firstElementChild;

p.className = "highlight";
} 

} });

Try asking the question directly from the challenge. This way Treehouse can build navigation so we can see where this is and go from there. Thanks

Steven Parker
Steven Parker
243,134 Points

Alternately, since you've already posted this one, provide a link to the course page you were working on.

1 Answer

Steven Parker
Steven Parker
243,134 Points

I'm working from memory since you didn't provide a link to the challenge, but I recall the instructions tell you to highlight the paragraph that comes before the button in the same list item. But I can see that this code would highlight the paragraph in the previous list item.

Hint: You're working too hard, the actual solution doesn't require traversing to the parent element or testing its position in the list.