oimoim oimoim - 2 months ago 9
HTML Question

JavaScript getting complete code of element when mouseover or onclick

How can I get the complete HTML code of the element I'm mouseovering or clicking?
Using

document.onmouseover = function(e){
can I get the full underlying HTML code of the element which triggered the event?

Right now, I am able to get the tag name or id or whatever.
What I would like is the whole code.

For example; if I'm mouseovering a table, I would like to get the string:
<table><tr><td></td></tr></table>


Is this possible?

Answer

IE already has element.outerHTML

and this for others:

if (!('outerHTML' in document.documentElement)) {
    function getOuterHTML(element){
        var dummy = document.createElement('html');
        dummy.appendChild(element.cloneNode(true));
        var result = dummy.innerHTML;
        dummy.innerHTML = '';
        return result;
    }
}
else{
    function getOuterHTML(element){
        return element.outerHTML;
    }
}

you can use it like this:

 alert( getOuterHTML( document.getElementById("elementID") ) );
Comments