JavaScript JavaScript and the DOM Traversing the DOM Sibling Traversal

michael edmondson
michael edmondson
3,369 Points

Not sure where im stuck at here

Can somoone please assist me with the error im making here ?

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

list.addEventListener('click', function(e) {
  if (e.target.tagName == 'BUTTON') {
  if(e.target.className == 'highlight'){
    let li = e.target.parentNode;
    let prevLi = li.previousElementSibling;
    let list = li.parentNode;
  }  
  }
});
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 class = "hightlight">Element Selection</p><button>Highlight</button></li>
                <li><p class = "highlight">Events</p><button>Highlight</button></li>
                <li><p class = "highlight">Event Listening</p><button>Highlight</button></li>
                <li><p class = "highLight">DOM Traversal</p><button>Highlight</button></li>
            </ul>
        </section>
        <script src="app.js"></script>
    </body>
</html>

1 Answer

Steven Parker
Steven Parker
177,655 Points

Here's a few hints:

  • you don't need to test the current class setting
  • you won't need to reference the parent for anything
  • the paragraph is the sibling of the button itself
  • once you traverse to the sibling, you'll set the class on it
  • you can traverse and set the class in a single statement
michael edmondson
michael edmondson
3,369 Points

sorry im still not getting you here still stuck

Steven Parker
Steven Parker
177,655 Points

Do any of the hints make sense? Try to incorporate as many as you can, and then show the new state of the code and we can continue from there.

michael edmondson
michael edmondson
3,369 Points

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

list.addEventListener('click', function(e) { if (e.target.tagName == 'BUTTON') { if (e.target.className == 'highlight'){ let prevLi = li.previousElementSibling; }
} });

Steven Parker
Steven Parker
177,655 Points

Let me see if I can explain the hints better, and also include them into the code:

  if (e.target.tagName == 'BUTTON') {
   // Hint: you don't need to test the current class setting
   if(e.target.className == 'highlight') {
   // what I meant by this is that this test isn't helpful.   The button probably will NOT
   // have this class but we don't need to know even if it does.  Remove this completely.

    // Hint: you won't need to reference the parent for anything
    // Hint: the paragraph is the sibling of the button itself
    let prevLi = li.previousElementSibling;
    // So instead of finding the sibling of the list item (which you no longer select anyway),
    // what you really want is the sibling of THE BUTTON.  We already know the button is
    // e.target, so you can use that to find the previous sibling.

    // Hint: once you traverse to the sibling, you'll SET the class on it
    paragraph.className = "highlight";
    // you might call it something other than "paragraph", but you must ASSIGN the class to it
   }  
  }

The final hint "you can traverse and set the class in a single statement" just means that you can combine the code that gets the sibling with the code that sets the class into one line. But this isn't necessary to pass the challenge.

michael edmondson
michael edmondson
3,369 Points

Still Stuck this is my code. what am i missing here? i even rewatch the videos i dont think i get what being asked here var list = document.getElementsByTagName('ul')[0];

list.addEventListener('click', function(e) { if (e.target.tagName == 'BUTTON') { let p = event.target.parentNode; let pPrev = p.previousElementSibling; p.className = "highlight";

} });

Steven Parker
Steven Parker
177,655 Points

Remember the 2nd hint, "you won't need to reference the parent for anything". The paragraph is the sibling of the button..

And you can't have two assignment operators in the same statement (but you don't need to create a variable at all if you assign the element directly).