Last active
September 21, 2023 13:31
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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