set img.src to dynamic svg element

Viewed 23093

I know that I can set a svg file as the src of an HTML img element like this:

<img src="mySVG.svg"/>

but can I somehow set a dynamic SVG element as the src of an img?

<svg id="mySVGElement">
    ...
</svg>

<img src="?"/>
2 Answers

This updates the accepted answer from Phrogz (8 years after it!)

The sample does not work on Chrome or Firefox (the image appears broken) changing to

  img.src = "data:image/svg+xml;base64,"+btoa(xml);

I am not sure of the reason why the original stopped working but this may help someone landing here.

Related