0

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!

1 Answer 1

1

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>");
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. Yeah, jQuery is cool and helps a lot. Unfortunately the code is for a js snippet, which should be integrated into other websites. So i should keep everything small and mustn't include some extra librarys.

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.