Get the string representation of a DOM node
You can create a temporary parent node, and get the innerHTML content of it:
var el = document.createElement("p");el.appendChild(document.createTextNode("Test"));var tmp = document.createElement("div");tmp.appendChild(el);console.log(tmp.innerHTML); // <p>Test</p>
EDIT: Please see answer below about outerHTML. el.outerHTML should be all that is needed.
What you're looking for is 'outerHTML', but wee need a fallback coz it's not compatible with old browsers.
var getString = (function() { var DIV = document.createElement("div"); if ('outerHTML' in DIV) return function(node) { return node.outerHTML; }; return function(node) { var div = DIV.cloneNode(); div.appendChild(node.cloneNode(true)); return div.innerHTML; };})();// getString(el) == "<p>Test</p>"
You'll find my jQuery plugin here: Get selected element's outer HTML
I dont think you need any complicated script for this. Just use
get_string=(el)=>el.outerHTML;