Skip to content

Instantly share code, notes, and snippets.

@hvianna
Last active September 21, 2023 13:31
Show Gist options
  • Save hvianna/3d277e79985a81b20db8758bf25d7637 to your computer and use it in GitHub Desktop.
Save hvianna/3d277e79985a81b20db8758bf25d7637 to your computer and use it in GitHub Desktop.
Embed external SVG image into the DOM so attributes (fill, background) can be manipulated via JavaScript
fetch( 'image.svg' )
.then( response => response.text() )
.then( content => {
const doc = new DOMParser().parseFromString( content, 'text/xml' ),
svg = doc.querySelector('svg');
svg.setAttribute( 'fill', '#d20a11' );
document.getElementById('image-container').append( svg );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment