Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get innerHTML of svg-tag result in undefined in IE

I'm having some trouble getting the html content of a svg-tag, with javascript in Internet Explorer.

My javascript code is as follow:

console.log($('.icon')[0].innerHTML);

and my tag in my html document:

<svg class="icon"><use>testing</use></svg>

This works well in Chrome, Firefox and what have we, but in Internet Explorer I'm left with an undefined error. Is it my mixture of Javascript and jQuery?

How come I can't seem to fetch the content of my svg-tag? I've tried some different things and often I end up with an "SCRIPT5007: Unable to set property 'innerHTML' of undefined or null reference"

Help me :) thanks in regards!

like image 255
aventic Avatar asked Jan 24 '15 20:01

aventic


2 Answers

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

console.log(new XMLSerializer().serializeToString(element));
like image 156
Robert Longson Avatar answered Oct 02 '22 10:10

Robert Longson


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.

like image 15
Sampson Avatar answered Sep 30 '22 10:09

Sampson