Javascript: I have the DOM representation of a node (element or document) and I\'m looking for the string representation of it. E.g.,
var el = document.creat
Under FF you can use the XMLSerializer
object to serialize XML into a string. IE gives you an xml
property of a node. So you can do the following:
function xml2string(node) {
if (typeof(XMLSerializer) !== 'undefined') {
var serializer = new XMLSerializer();
return serializer.serializeToString(node);
} else if (node.xml) {
return node.xml;
}
}
If your element has parent
element.parentElement.innerHTML
I dont think you need any complicated script for this. Just use
get_string=(el)=>el.outerHTML;
Try
new XMLSerializer().serializeToString(element);
if using react:
const html = ReactDOM.findDOMNode(document.getElementsByTagName('html')[0]).outerHTML;