Skip to content

Instantly share code, notes, and snippets.

@ivanhoe011
Last active February 20, 2016 14:19
Show Gist options
  • Save ivanhoe011/87c1679a3da8fddd2f66 to your computer and use it in GitHub Desktop.
Save ivanhoe011/87c1679a3da8fddd2f66 to your computer and use it in GitHub Desktop.
Add/ Remove classes on SVG
/**
* Usage (where #map is ID of some element in svg document):
* $('#map').addSVGClass('some-class');
* $('#map').removeSVGClass('some-other-class');
*/
(function ($) {
$.fn.addSVGClass = function(className) {
var re = new RegExp('\\b'+ className +'\\b');
return this.each( function() {
var c = $(this).attr('class');
if ( !c || !c.match(re)) {
c = (!c ? '' : c +' ') + className;
$(this).attr('class', c);
}
});
};
$.fn.removeSVGClass = function(className) {
var re = new RegExp('\\s*\\b'+ className +'\\b\\s*');
return this.each( function() {
var c = $(this).attr('class');
if (c) {
$(this).attr('class', c.replace(re, ''));
}
});
};
}( jQuery ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment