g_ap
g_ap

Reputation: 83

adding property to array element in javascript

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

Answers (3)

vol7ron
vol7ron

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

fred02138
fred02138

Reputation: 3371

The problem is that the name title is reserved. Try a different name.

Upvotes: 1

tymeJV
tymeJV

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

Related Questions