Reputation: 15230
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.
Upvotes: 1
Views: 1516
Reputation: 14
The simple way for converting String to DOM and vice versa is presented on this link:
Upvotes: 0
Reputation: 389
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!
Upvotes: 0
Reputation: 2452
There can be more possible cases. You should clarify a bit.
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.
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");
Upvotes: 0