Gudron Swiss
Gudron Swiss

Reputation: 445

appendChild give an error, JavaScript

I found a lot of questions here about this problem, but i still can't figure out how to fix it in my case. Now i created a new html file and have there only this code:

<script>
window.onload = function() {
     var obj = new Object;
         document.getElementById("a").appendChild(obj);
    };
</script>

I tryed to place it in different places, with and without "onload" function, using getElementById or just document.body, but i get an error: "Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist."

Thank you, guys.

Upvotes: 0

Views: 161

Answers (1)

geevee
geevee

Reputation: 5451

appendChild will work only with a DOM element. try:

var obj = document.createElement('div');

instead of:

var obj = new Object;

(btw, need to use new Object() and not new Object)

hope that helped.

Upvotes: 3

Related Questions