Skip to content

Instantly share code, notes, and snippets.

@lfarroco
Last active August 29, 2015 14:16
Show Gist options
  • Save lfarroco/0047cd96348c9801a195 to your computer and use it in GitHub Desktop.
Save lfarroco/0047cd96348c9801a195 to your computer and use it in GitHub Desktop.
//Extract SVG: three lines of JavaScript that allow you to extract the contents
//of a SVG <object> tag into your DOM
//After the extraction you are able to select the inner elements of the SVG with CSS and JS
//It replaces the original <object> tag with its raw SVG
//el: a DOM element
//eg. el = document.querySelector('#elementId')
//extractSvg(el)
//You might also place an onload attribute in your object to force its extraction when possible
//<object data="test.svg" type="image/svg+xml" onload="extractSvg(this)"></object>
function extractSvg(el) {
var svg = el.contentDocument;
svg = svg.documentElement;
el.parentNode.replaceChild(svg,el);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment