user160820
user160820

Reputation: 15230

Converting String to DOM

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

Answers (5)

ti regist
ti regist

Reputation: 14

The simple way for converting String to DOM and vice versa is presented on this link:

http://famulatus.com/ks/programming/java/item/335-convert-xml-dom-to-string-and-string-to-xml-dom.html

Upvotes: 0

z-index
z-index

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

Ervin
Ervin

Reputation: 2452

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"); 

Upvotes: 0

Danil
Danil

Reputation: 1893

you can use innerText to do it

Upvotes: 1

Delan Azabani
Delan Azabani

Reputation: 81492

parentNode.innerHTML += responseText;

Upvotes: 6

Related Questions