Explore Bato Web Agency Webflow Certified Partner Profile Explore Bato Web Agency Webflow Certified Partner Profile

Failed to execute ‘insertBefore’ on ‘Node’: parameter 1 is not of type ‘Node’

Illustration without a description Illustration without a description

The first argument to insertBefore() must be the DOM node to insert. A string or component description cannot stand in for that node. Inspect the second argument too. It must be a child of the receiver, or null when the node should go at the end.

 

Create or retrieve the DOM element first. Pick the reference from the parent that will receive it. In this example, parent.firstChild is the place to insert before.

 

const parent = document.querySelector('#list'); const newItem = document.createElement('li'); newItem.textContent = 'First'; parent?.insertBefore(newItem, parent.firstChild);

 

Try the same insertion with a populated list and an empty list. The first child is the reference in the first case. It is null in the second, so the new element is inserted at the end.

 

Inspect both insertBefore arguments and check that the reference node is still a child of the intended parent.