I have my SVG definitions in a separate file. I can embed them into the HTML like so:
<svg class="icon icon-close"><use xlink:href="/icons/symbol-defs.svg#icon-close"></use></svg>
How can I embed these icons from this separate file with javascript?
I have tried writing the SVG string to the DOM with the following:
var icon = '<svg class="icon icon-close"><use xlink:href="/icons/symbol-defs.svg#icon-close"></use></svg>';
$('#myDiv').append(icon);
But the icon does not appear.
I have looked at answers like this, which tells me that SVG elements are different to normal DOM elements (as I need to use .createElementNS
). However as all my definitions are in this separate file, I don't want to have to duplicate the SVG definitions inside the javascript.
Is there a way of getting Javascript to read from my symbol-defs.svg
file and create an SVG and place it dynamically into the DOM?
hi
');` then that `H1` tag appears fine - the SVG does not however – Jimmery Feb 20 '17 at 11:45