Why can not we get outerHTML of an svg element with element.outerHTML
property?
Is this way is the best http://jsfiddle.net/33g8g/ for getting svg source code?
Why can not we get outerHTML of an svg element with element.outerHTML
property?
Is this way is the best http://jsfiddle.net/33g8g/ for getting svg source code?
It's not accessible via outerHTML because SVG is not HTML -- it's a separate XML specification.
That's why, for example, your svg node in that example has its own namespace defined (
xmlns="http://www.w3.org/2000/svg
).Your example may be the most expedient for a one-off query, but it's actually possible to dig in using native attributes. It just takes a bit more work.
Let's use the linked sample node:
If you want to extract the namespace and version, use the
attributes
property.If you want to extract the actual contents, iterate over the children. Similar to the above, a non-text node's
attributes
collection will contain the x/y values (etc).Without using jQuery, using your example again:
Here's an updated Fiddle, a bit more elegantly demonstrating the possibilities: http://jsfiddle.net/33g8g/8/
This is an easier solution and it works great in FF, Chrome, IE. Honor goes to Philipp Wrann.
outerHtml is not working in IE
2013 update: The
innerHTML
andouterHTML
are going to be supported for svg elements too, per the DOM Parsing specification.A patch for this has been landed in Blink/Chrome and will become available soon, see http://code.google.com/p/chromium/issues/detail?id=311080.
Using jQuery, you can easily create a temporary HTML wrapper around any element that doesn't support outerHTML :
SVGElements don't have outerHTML property.
You can define like this in pure Javascript
Or a one line jQuery solution would be
Reference: https://gist.github.com/jarek-foksa/2648095