node package manager

svg-inject

svg-inject experimental

Replace an <img> element with an inline SVG. This way you can still earn the benefits of not inlining every occurence of an SVG, but still style their contents using CSS selectors. This makes adding hover states and the like much more straightforward :)

Usage

NPM

inject(element, [callback])

Replace the <img> element with an SVG, calling callback(err, svg) when complete.

// Replaces all existing SVG images on the DOM with inline SVGs: 
var inject = require('svg-inject')
var svgs = document.querySelector('img[src$=".svg"]')
 
for (var i = 0; i < svgs.length; i++) {
  inject(svgs[i], function(err, svg) {
    if (err) throw err
    // do additional things with `svg` here if you like. 
  })
}

See Also

License

MIT. See LICENSE.md for details.