Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

False "the node before which the new node is to be inserted is not a child of this node."

None of the other related questions had a working answer, thus I decided to ask here myself.

While writing JavaScript I keep getting the following error no matter what I try:

Failed to execute ‘insertBefore’ on ‘Node’: The node before which the new node is to be inserted is not a child of this node.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the line of code I have, after replacing variable names:

newElement.insertBefore(document.body, aChildOfDocumentBody);

Both newElement and aChildOfDocumentBody are defined with getElementById.

Even when I try newElement.insertBefore(document.body, document.body.children[0]) JavaScript says that the child doesn’t belong to document.body. What am I doing wrong?

>Solution :

This seems to be just a misordering of variables in your function call

newElement.insertBefore(document.body, document.body.children[0])

Should (I think) be

document.body.insertBefore(newElement, document.body.children[0]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading