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

Methee Saengow
Methee Saengow
5,474 Points

Anyone can help me do this test. It is too hard for me T_T. I think I don't quite understand the concept of it

Anyone can help me do this task. It is too hard for me T_T. I think I don't quite understand the concept of it as the the lessons taught previously may use too much complex examples and too fast for me to catch up.

But I think that this task (that I can't do) should be a simple task that if i know the answer, it will make me more understand the concept.

Please help

Methee S.

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

list.addEventListener('click', function(e) {
  if (e.target.tagName == 'BUTTON') {

  }
});
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>

2 Answers

Ok, I just passed this one...Without pasting my code, my understanding is you are telling the program to highlight an item in the list when the corresponding button is pressed, by adding a className of "highlight" to the previousElementSibling of the button, which is the <p>. Make sure you use "className", not "class", or it won't work. Hope this helps!

tomd
tomd
16,701 Points

I'll tell you the answer in plain English, you'll have to translate it to code.

  1. You'll want to select the buttons previous sibling (HINT: You'll need to actually select the button first... e.target.SELECT_SIBLING_SOMEHOW?)

  2. Then you'll need to add a class to the buttons sibling (the <p>)

  3. You'll need something in the css to actually apply the style. So if you add the class highlight you'll need .highlight in your css with a background-color?