Skip to content

Instantly share code, notes, and snippets.

@ipatch
Created March 19, 2019 15:11
Show Gist options
  • Select an option

  • Save ipatch/f5695854a2f70212ebc5b0a5122f1b24 to your computer and use it in GitHub Desktop.

Select an option

Save ipatch/f5695854a2f70212ebc5b0a5122f1b24 to your computer and use it in GitHub Desktop.
add svg to DOM using Javascript
// I'm using Webpack to build a bundle of a simple index.js
// ...and I'm trying to add a SVG element to the DOM using Webpack, which I believe is working,
// ...however Webpack is unable to locate the path to the SVG or Webpack is not properly handling the SVG
// setup namespace for SVG
var svgns = 'http://www.w3.org/2000/svg';
var svg = document.createElementNS(svgns, 'svg');
document.body.appendChild(svg);
svg.setAttribute('version', '1.1');
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
// baseProfile
// width
// height
// bring in external svg
var use1 = document.createElementNS(svgns, "use");
use1.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', './images/purple-circle-plain.svg#circle-purple');
svg.appendChild(use1);
@ipatch
Copy link
Author

ipatch commented Mar 19, 2019

Screen Shot 2019-03-19 at 10 11 56 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment