Error with appendChild: Node cannot be inserted at the specified point in the hierarchy
Asked Answered
L

1

13

There is an error with the function appendChild : Node cannot be inserted at the specified point in the hierarchy

JS :

var abc=document.createElement("div");
abc.style.position="absolute";
abc.style.width="10px";
abc.style.height="10px";
abc.style.left="10px";
abc.style.top="10px";
abc.style.backgroundColor="black";
abc.innerHTML="abc";
document.appendChild(abc);

http://jsfiddle.net/T7ZMX/

Can you please help me?

Laniferous answered 24/5, 2012 at 19:32 Comment(1)
In case anyone else also has this issue, I was accidentally using document.createAttribute() when i meant to use document.createElement()Thremmatology
H
32

You need to append to document.body, not just document.

To explain why document.appendChild doesn't work consider the following diagram :

DOM Tree

If that would be allowed that wouldn't be very useful since it will be a sibling of the HTML root element, that make it totally outside the content.

For more information : Using the W3C DOM Level 1 Core

Haviland answered 24/5, 2012 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.