Get innerHTML of svg-tag result in undefined in IE

Internet Explorer doesn't currently support the innerHTML method on SVG Elements. We do however have an issue opened internally to track our consideration of this feature. In the meantime I would explore alternative solutions like the InnerSVG polyfill which allegedly works in Internet Explorer 9 and newer.


How about using XMLSerializer. Get yourself the element somehow and then do this...

console.log(new XMLSerializer().serializeToString(element));

The following code will give you the content as string from the given SVG. It is based on the great answer of Robert Longson.

const svgNode = document.getElementsByTagName('svg')[0];
const serializer = new XMLSerializer();

const svgContent = Array.prototype.map.call(
  svgNode.childNodes, 
  (child) => serializer.serializeToString(child)
).join('');