1

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?

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to create that div as a property of d[0]?
@user2799563 -- d[0] is a position in an array, that currently holds a div, use d[1]
1

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

Comments

0

.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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.