Bummer! You have been redirected as the page you requested could not be found.

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

Brian Thomas
Brian Thomas
10,688 Points

Not sure how this code is not adding the class name highlight to the p tags. Can someone help explain?

.

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

list.addEventListener('click', function(e) {
  if (e.target.tagName == 'BUTTON') {
    let ul = e.target.parentNode;
    let p = e.target.previousElementSibling;
    p.addClass('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>

1 Answer

Brian, I think the problem is that the addClass() method comes from jQuery. I don't think it exists in vanilla javascript. I think you can do p.className = 'highlight' , and I think that would be accepted as an answer to the quiz, but of course it has the unfortunate side effect that if the paragraph had a class value, it would be replaced, not added to.

I am kind of new to javascript, so this answer is provisional.

Brian, I can confirm that replacing p.AddClass('highlight') with p.className = 'highlight'; will be correct for this code challenge!