I recently looked for something similar to this, but I needed to update part of the existing SVG, so using innerHTML was not an option for me. I ended up watching Canvg pull it out and come up with the following:
var $contentToReplace = $('svg .some-class'); $contentToReplace.empty(); var content = new DOMParser().parseFromString(newContent, 'text/xml');
You can check how they did it in Canvg - just find the parseXml method.
You can also pass the parseFromString method to another MIME method. The browser support for them changes a little, although, for example, the MIME type "image / svg + xml" is not supported in IE9 or earlier.
Changing the MIME type simply changes the type of document you are returning. Using 'text / xml' returns an XMLDocument, which works fine in this case.
source share