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

J.D. Williams
J.D. Williams
17,816 Points

Can one use the `before` and `after` methods on the target sibling node instead of `insertBefore`?

The initial solution provided by the instructor made sense to me, but it was not the most intuitive approach. Therefore, I read the MDN docs and refactored the code for the 'up' and 'down' buttons as indicated below.

// Move `li` up list
if (event.target.className === "up") {
  let li = event.target.parentNode;
  let prevLi = li.previousElementSibling;
  if (prevLi) {
    prevLi.before(li);
  }
}
// Move `li` down list
if (event.target.className === "down") {
  let li = event.target.parentNode;
  let nextLi = li.nextElementSibling;
  if (nextLi) {
    nextLi.after(li);
  }
}

The functionality of the buttons, based on the above snippets, is equivalent to the original solutions. Can anyone identify a condition I may have overlooked that would cause the buttons to not manipulate the DOM as expected, or is this fine as is? Thank you in advance.

1 Answer

Your code looks fine to me, maybe try equality operator instead of strict equality.

J.D. Williams
J.D. Williams
17,816 Points

Thank you for the feedback.