Convert DOM to String
Posted: February 15th, 2008 | Author: Nischal Shetty | Filed under: JavaScript | 4 Comments »While working on one of my projects I came across a scenario where I needed to convert the DOM object into a String representation to send it as an AJAX request parameter.
The way to do this is as follows:
Suppose you have a form named “Test”.
We first get the DOM object of the current page as follows:
var domObject = document.Test;
//Now lets convert this DOM to String
var domToString = domObject.innerHTML;
Thats it!!! Put an alert and you’ll see the complete DOM in a string representation.
alert(domToString);












Minor point: this apparently doesn’t work for XML documents.
@John
Thnks for the info
For XML, you can just do:
var str = (new XMLSerializer()).serializeToString(domObject);
@Timo cool, thanks for the info.