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 Solution: Using nextElementSibling

Is my solution to the problem ok?

I came up with a different solution to this problem, it seems to be working great regardless of the number of 'li' elements and I didn't have to use a second 'if' statement. Would this be ok? Thanks!!

if (event.target.className == 'down') { let li = event.target.parentNode; let nextLi = li.nextElementSibling.nextElementSibling; let ul = li.parentNode; ul.insertBefore(li, nextLi); }

The if statements are used to check "Is there another element before this one?" If yes, then perform the move, if not, do nothing.

I imagine in your version, pressing the up button on the top li element will move it to the bottom of the list.

1 Answer

As far as I see clicking the last 'down' button will throw a error in the console. As there is no check to see if the 'li' exists.

Now if you add a check to prevent that error, your 'down' feature won`t work for the button before the last one as li.nextElementSibling.nextElementSibling; will return null.