Skip to content

Instantly share code, notes, and snippets.

@nborko
Last active March 28, 2016 18:38
Show Gist options
  • Save nborko/5943c44ec0a58d9b915d to your computer and use it in GitHub Desktop.
Save nborko/5943c44ec0a58d9b915d to your computer and use it in GitHub Desktop.
jquery shims for [add|remove|has]Class for svg
/* jquery shims for [add|remove|has]Class for svg */
(function ($) {
$.fn.addClass = (function(addClass) {
return function(value) {
return addClass.apply(this, arguments).each(function () {
if (this instanceof SVGElement) {
$(this).attr('class', $('<div/>', {'class': $(this).attr('class')}).addClass(value).attr('class'));
}
});
};
})($.fn.addClass);
$.fn.removeClass = (function(removeClass) {
return function(value) {
return removeClass.apply(this, arguments).each(function () {
if (this instanceof SVGElement) {
$(this).attr('class', $('<div/>', {'class': $(this).attr('class')}).removeClass(value).attr('class'));
}
});
};
})($.fn.removeClass);
$.fn.hasClass = (function(hasClass) {
return function(value) {
return hasClass.apply(this, arguments) || this.map(function () {
if (this instanceof SVGElement) {
return $('<div/>', {'class': $(this).attr('class')}).hasClass(value) || null;
}
}).length > 0;
};
})($.fn.hasClass);
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment