/html-dom /Basic
GitHub 5008★

Insert an element after or before other element

Insert after

Insert the ele element after the refEle element:

refEle.parentNode.insertBefore(ele, refEle.nextSibling);

// Or
refEle.insertAdjacentElement('afterend', ele);

Insert before

Insert the ele element before the refEle element:

refEle.parentNode.insertBefore(ele, refEle);

// Or
refEle.insertAdjacentElement('beforebegin', ele);

See also

Follow me on and to get more useful contents.