
Originally Posted by
mnaseersj
Writing the insertAfter function
There is no need to write an insertAfter function.
Why? The nextSibling property returns null if there is no next sibling.
When the insertBefore has null as the referenc element, it automatically appends instead.
For confirmation, this is what the insertBefore documentation page has to say:
Syntax
var insertedElement = parentElement.insertBefore(newElement, referenceElement);
If referenceElement is null, newElement is inserted at the end of the list of child nodes.
So, the following is the only code that you need to properly insert after an element, regardless of whether that element has any siblings after it or not.
Code javascript:
targetElement.parentNode.insertBefore(newElement, targetElement.nextSibling);
Bookmarks