JavaScript JavaScript and the DOM Traversing the DOM Solution: Using nextElementSibling

I wrote it two ways, both work

ul.insertBefore(nextLi, li);

that line can be replaced with:

li.after(nextLi, li);

i understand the first one just reverses the up button but can someone explain what the second version is doing?

1 Answer

Steven Parker
Steven Parker
194,522 Points

The "after" method is another way to add an element to the DOM; but as you apparently noticed already, it's applied to a sibling instead of a parent.

Also, according to the MDN page, this method is classified as experimental, so it would not be recommended for use in production code.