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

Why did setting the className to 'highlight' actually highlight the elements in the exercise?

I thought actually making changes to style had to be added with additional properties and not just by naming a class 'highlight.' I don't see any CSS and the highlight is applied in a stylized green. What's going on here? Help!

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

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

1 Answer

Jamie Moore
Jamie Moore
3,997 Points

Hi Shawn,

You are right! There is a further file, 'style.css', which is being included in the head of the HTML file <link rel="stylesheet" href="style.css" />.

Although I can't see what it's in this, I would assume there are some styles from this file that are being applied to a highlight class.

Hope this helps!

It absolutely helps. I saw the link to the style sheet and just hoped that was the answer. Little things like this tend derail me for a bit, as I aim to understand every piece of a concept before moving on. So, thanks so much for the prompt response!