Last active
February 20, 2016 14:19
-
-
Save ivanhoe011/87c1679a3da8fddd2f66 to your computer and use it in GitHub Desktop.
Add/ Remove classes on SVG
This file contains hidden or 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
| /** | |
| * 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