JavaScript JavaScript and the DOM Traversing the DOM Sibling Traversal

Julianna Kahn
Julianna Kahn
20,700 Points

My understanding is that the paragraph would be the previousElementSibling to the button?

Am I defining p correctly?

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

list.addEventListener('click', function(e) {
  if ( == 'BUTTON') {
    let button =;
    let p = button.previousElementSibling;
    p.className = 'highlight';
<!DOCTYPE html>
        <title>JavaScript and the DOM</title>
    <link rel="stylesheet" href="style.css" />
            <h1>Making a Webpage Interactive</h1>
            <p>Things to Learn</p>
                <li><p>Element Selection</p><button>Highlight</button></li>
                <li><p>Event Listening</p><button>Highlight</button></li>
                <li><p>DOM Traversal</p><button>Highlight</button></li>
        <script src="app.js"></script>

3 Answers

Steven Parker
Steven Parker
200,760 Points

You have the right idea, and you're doing the right thing with "p".

But the button is not "", but simply "".

Jason Anders
Jason Anders
Treehouse Moderator 145,037 Points

HI Julianna Kahn

For this one, I'm going to direct to a very well explained answer provided by Antony Pesantez in this older post.

Keep Coding! :) :dizzy:

Julianna Kahn
Julianna Kahn
20,700 Points

That is helpful, thank you.

Julianna Kahn
Julianna Kahn
20,700 Points

That makes a lot of sense, thank you.