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 (Retiring) Traversing the DOM Sibling Traversal

Jonathan Drake
seal-mask
.a{fill-rule:evenodd;}techdegree
Jonathan Drake
Full Stack JavaScript Techdegree Student 11,668 Points

Newbie trouble with previousElementSibling Challenge

For the life of me I cannot figure out how to make this work. The whole parentNode thing is still somewhat of a mystery to me, so I'm sure that my difficulty stems from a lack of comprehension of what needs to happen. Using my notes from the previous videos I tried to just apply code I've already seen to what this challenge requires. The error I get is that I'm not selecting a previous sibling. This could also come from not fully understanding the nature of the DOM. As I see it the <button> is a sibling of the <p> which are children of the <li> which is a child of the <ul>... I'm at the point in this unit where I just watch solutions and take lots of notes because I don't have enough of a grasp of the material to improvise. That came later for me in Unit 1...just takes me while. Thanks for any help

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

list.addEventListener('click', function(e) {
  if (e.target.tagName == 'BUTTON') {
    let li = e.target.parentNode;
    let p = li.previousElementSibling;
    let ul = li.parentNode;
    if (p) {
       ul.className = 'highlight';
    }
  }
});
index.html
<!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>

3 Answers

Hi. understand the previesElementSibling is not easy. previousElementSibling just select the previous element of any element.. so it means if you select the button element previous element will be the <p> element. if you select the <p> element previous element will be the <li> element. I checked your code and there is a problem that when you do this: let p = li.previousElementSibling; will select the previous <li> element. Not the <p> element. A good answer for this question will be:

list.addEventListener('click', function(e) {
  if (e.target.tagName == 'BUTTON') {
    let p = e.target.previousElementSibling;
    p.className = 'highlight';
  }
});

I hope this helped you.

Hi Jonathan!

This passed for me:

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

list.addEventListener('click', function(e) {
  if (e.target.tagName == 'BUTTON') {
    let p = e.target.previousElementSibling;
    p.className = "highlight";
  }
});

(let p = e.target.previousSibling; passed as well, but I think let p = e.target.previousElementSibling; is better.)

I hope that helps.

Stay safe and happy coding!

Jonathan Drake
seal-mask
.a{fill-rule:evenodd;}techdegree
Jonathan Drake
Full Stack JavaScript Techdegree Student 11,668 Points

Thanks. I was overcomplicating it. This worked perfectly. I was getting tangled up with the parentNode stuff. Still don't fully understand how that works. Just not second-nature yet. Thanks for the reply!

Hey Stefan!

Jinx!?!

(I didn't plagiarize!?! LOL)

Great minds think alike!

-Pete

:)