views:

45

answers:

4

My Ajax functions gives me HTML-Elements as String back, and this String I want to append in my Document as DOM Element.

Something like

parentNode.appendChild(responseText);

What will be the best way to do this.

+6  A: 
parentNode.innerHTML += responseText;
Delan Azabani
+1  A: 

you can use innerText to do it

Danil
Cant use innerText to append DOM since it will append it as TEXT. He must use innerHTML to append that string as HTML (DOM)! Since you are new no -1, but in future...
Cipi
yep, you are right. sorry.
Danil
A: 

There can be more possible cases. You should clarify a bit.

  1. If you get a string that should be an object and it's not existing yet, then you should use this: var tempObj = document.createElement("yourString"); then you can just use tempObj to handle it.

  2. If you get a string that is the name or ID of an existing object, then use: var tempObj = document.getElementByName("yourString");

or

var tempObj = document.getElementById("yourString"); 
Ervin
A: 

If you are using prototype, you can use the DOM methods this library provides, for example the insert() or update() method:

$('parentId').insert(yourString);

or

$('parentId').update(yourString);

http://api.prototypejs.org/dom/element/insert/

http://api.prototypejs.org/dom/element/update/

Note that innerHTML is not standarized yet, so using prototype, you can be sure those methods are cross browser compatible.

Good luck!

Koder_