Reputation: 83
I'm trying to learn and understand javascript.
what is wrong with the following code?
var d=[];
d[0]=document.createElement('div');
d[0].title=document.createElement('div');
d[0].appendChild(d[0].title);
I get this error: TypeError: Argument 1 of Node.appendChild is not an object.
Can you suggest a solution?
Upvotes: 1
Views: 2713
Reputation: 42179
.title
is an attribute of the element, which is a string. When you try to append something to that attribute it is expecting a string.
Upvotes: 0
Reputation: 3371
The problem is that the name title
is reserved. Try a different name.
Upvotes: 1
Reputation: 104795
This line d[0].appendChild(d[0].title);
is expecting an element to be appended to the div. Your simply appending a text node. Create another div
(or whatever element you want) and append that.
Upvotes: 2