Alex B.
Alex B.

Reputation: 947

DOM Manipulation - append a string-variable with dom content

haven't found a solution for my problem. I have to admit that i had problems to find the correct search terms. It seems i don't see the forest for the trees - once again...

I'm trying to append a child element, which isn't spectacular at all. But now i have a variable (ajax - json) which already contains a dom element (<a href="xx"><img src="xx" /></a>) and i wan't to add this content.

First approach elem.appendChild(var);

throws this error: *NOT_FOUND_ERR: DOM Exception 8*

Next try elem.appendChild(document.createTextNode(var)); encodes the entities. So the anchor and img-tag are printed plain.

any help? thanks in advance!

Upvotes: 0

Views: 411

Answers (1)

Matthew Abbott
Matthew Abbott

Reputation: 61589

You're trying to using appendChild with a textural representation of elements, not a dom node itself. Try using the innerHTML property:

elem.innerHTML += "<a href=\"xx\"><img src=\"xx\" /></a>";

JQuery has a nice append method which takes care of all this for you:

$("div.someDiv").append("<a href=\"xx\"><img src=\"xx\" /></a>");

Upvotes: 1

Related Questions