[Home] > Snippets  > Browser  > DOM  >  Insert an element before other one

Insert an element before other one

JavaScript

const insertBefore = (ele, anotherEle) => anotherEle.parentNode.insertBefore(ele, anotherEle)

// Or
const insertBefore = (ele, anotherEle) => anotherEle.insertAdjacentElement('beforebegin', ele)

TypeScript

const insertBefore = (ele: Element, anotherEle: Element): Element | null => (anotherEle.parentNode ? anotherEle.parentNode.insertBefore(ele, anotherEle) : null)

// Or
const insertBefore = (ele: Element, anotherEle: Element) => anotherEle.insertAdjacentElement('beforebegin', ele)