JavaScript JavaScript and the DOM Traversing the DOM Sibling Traversal

Joseph Chauvin
Joseph Chauvin
Full Stack JavaScript Techdegree Student 8,659 Points

Why is my code not working?

I do not understand why this line of code is not working:

p.className = "highlight";

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

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

1 Answer

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 47,285 Points

Just a spelling error:

previousElementSibiling

should be

previousElementSibling