2

Let's say I have arrow.svg file. In my index.js that is in vanilla JS, I want to appendChild this to a body. How do I do that?

const theSVGFile = //... read from './arrow.svg'
document.getElementById('some-id').appendChild(theSVGFile);
Kousha
  • 32,871
  • 51
  • 172
  • 296
  • You can set the path of the SVGfile to the src of an img tag. ex: – Zoilo Reyes Jul 16 '19 at 17:05
  • Possible duplicate of [JavaScript createElement and SVG](https://stackoverflow.com/questions/3492322/javascript-createelement-and-svg) This works you can appendChild when you create a SVG element – dota2pro Jul 16 '19 at 17:10

1 Answers1

0

An example of Zoilo's suggestion which is generally the easiest route but reality is he deserves the credit since he beat me to it.

const daSVGHost = document.createElement("img");
daSVGHost.src = "https://i.stack.imgur.com/BVW9D.jpg"; // SVG HERE

document.getElementById('testerooni').appendChild(daSVGHost);
<div id="testerooni"></div>
Chris W.
  • 22,835
  • 3
  • 60
  • 94