1

Say I create an image object like below. ( create(); is a function I wrote but I know this function works.)

var img = create("img", "images/duba.jpg");

When I try to put this image into the innerHTML of another object that's a div, I get this plain text where the image should show up:

[object HTMLImageElement]

What's the correct method to insert one object into another like this? I have to think there's a more graceful method than hard-coding the innerHTML with strings, which I've tried successfully.

5 Answers 5

3

Call appendChild to append the DOM element to an existing element.

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

Comments

1

You should use DOM manipulation methods. E.g.

Comments

1

I think you need to use:

otherObject.appendChild(img);

Comments

1

If you are creating an element you can't set it as a node's innerHTML, that takes a string of HTML. Just use DOM manipulation

parent.appendChild ( create("img", "images/duba.jpg") );

Comments

1

Use the appendChild method. The innerHTML property receives a string, not a DOM element.

https://developer.mozilla.org/En/DOM/Node.appendChild

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.