Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do OuterHTML in firefox?

Part of my code I get the OuterHTML propery

"<LI onclick="TabClicked(this, 'SearchName', 'TabGroup1');">Name " 

so I can do stuff involing parsing it.

There is no OuterHTML property in javascript on firefox though and I can't find an alternative way to get this string. Ideas?

like image 381
NibblyPig Avatar asked Nov 09 '09 13:11

NibblyPig


People also ask

How do I access outerHTML?

Using jQuery With jQuery, you can access the outerHTML attribute of HTML using the $(selector). prop() or $(selector). attr() method.

Is there an outerHTML?

The outerHTML attribute of the Element DOM interface gets the serialized HTML fragment describing the element including its descendants. It can also be set to replace the element with nodes parsed from the given string.

What is difference between innerHTML and outerHTML?

The outerHTML is the HTML of an element including the element itself. Use the outerHTML when you want to completely replace an element and its contents. Use innerHTML when you only want to replace the contents of the element.

How do you edit an element in HTML?

Editing HTML You can edit the HTML — tags, attributes, and content — directly in the HTML pane: double-click the text you want to edit, change it, and press Enter to see the changes reflected immediately. You can add any HTML in here: changing the element's tag, changing existing elements, or adding new ones.


1 Answers

Here's the function we use in pure.js:

function outerHTML(node){     return node.outerHTML || new XMLSerializer().serializeToString(node); } 

To use it the DOM way:

outerHTML(document.getElementById('theNode')); 

And it works cross browsers

EDIT: WARNING! There is a trouble with XMLSerializer, it generates an XML(XHTML) string.
Which means you can end up with a tags like <div class="team" /> instead of
<div class="team"></div>
Some browsers do not like it. I had some pain with Firefox 3.5 recently.

So for our pure.js lib we came back to the old and safe way:

function outerHTML(node){     // if IE, Chrome take the internal method otherwise build one   return node.outerHTML || (       function(n){           var div = document.createElement('div'), h;           div.appendChild( n.cloneNode(true) );           h = div.innerHTML;           div = null;           return h;       })(node);   } 
like image 194
Mic Avatar answered Sep 23 '22 20:09

Mic