Skip to content

Instantly share code, notes, and snippets.

@SashaKolbasov
Last active January 8, 2017 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SashaKolbasov/9bcb906da5d80376665a1f62dcd528c5 to your computer and use it in GitHub Desktop.
Save SashaKolbasov/9bcb906da5d80376665a1f62dcd528c5 to your computer and use it in GitHub Desktop.
Change SVG Color
$('img[src$=".svg"]').each(function () {
var $img = $(this),
imgID = $img.attr('id'),
imgClass = $img.attr('class'),
imgURL = $img.attr('src');
$.get(imgURL, function (data) {
var $svg = $(data).find('svg');
if (typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
if (typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass + ' replaced-svg');
}
$svg = $svg.removeAttr('xmlns:a');
if (!$svg.attr('viewBox') && $svg.attr('height') && $svg.attr('width')) {
$svg.attr('viewBox', '0 0 ' + $svg.attr('height') + ' ' + $svg.attr('width'))
}
$img.replaceWith($svg);
}, 'xml');
});
svg {
path {
fill: #000;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment