Custom insertAfter() Challenge
Instructions
You may think that since there is an insertBefore() method, there is also an insertAfter(), but there isn't. In this challenge, I want you to create a custom insertAfter() function. If you don't want to do it as a challenge, that's fine, just follow along.
- The first param will be
newElwhich will be a new element that you create withdocument.createElement() - The second param will be
existingElwhich is an element in the DOM that you want to insert your new element after
The function will be called like this:
// New element to insert
const li = document.createElement('li');
li.textContent = 'Insert Me After!';
// Existing element to insert after
const firstItem = document.querySelector('li:first-child');
// Our custom function
insertAfter(li, firstItem);
Hint:
Remember the properties to get parent and sibling elements. Use some of those combined with insertBefore().
Click For Solution
function insertAfter(newEl, existingEl) {
existingEl.parentElement.insertBefore(newEl, existingEl.nextSibling);
}
The solution is actually really simple.
- We take the
existingEland get the parent with either theparentElementorparentNodeproperty. - We call
insertBefore()on the parent and then pass innewEland the element AFTERexistingEl. We can get that with thenextSiblingproperty
Putting our new element before the existing element's next sibling is the same as putting it after the existing element