Javascript getElementById and convert it to String

Is there a way to convert a javascript HTML object to a string? i.e.

var someElement = document.getElementById("id");
var someElementToString = someElement.toString();

thanks in advance

+5
source share
5 answers

If you need a string representation of the entire tag, you can use it outerHTMLfor browsers that support it:

var someElementToString = someElement.outerHTML;

For other browsers, obviously, you can use XMLSerializer :

var someElement = document.getElementById("id");
var someElementToString;

if (someElement.outerHTML)
    someElementToString = someElement.outerHTML;
else if (XMLSerializer)
    someElementToString = new XMLSerializer().serializeToString(someElement); 
+13
source

You can always wrap a clone of an element in an "off screen", an empty container. The innerHTML container is the "outerHTML" of the clone and the original. Pass true as the second parameter to get the children of the elements.

document.getHTML=function(who,deep){ 
 if(!who || !who.tagName) return '';
 var txt, el= document.createElement("div");
 el.appendChild(who.cloneNode(deep));
 txt= el.innerHTML;
 el= null;
 return txt;
}
+2
source
someElement.innerHTML
+1

, element.innerHTML HTML- childnodes HTML. IE, outerHTML propoerty, - HTML

0

, . , , .

var message = ""; 
message = document.getElementById('messageId').value;

.. .

-1

All Articles