Skip to content

Instantly share code, notes, and snippets.

@mfurlend
Created July 5, 2016 23:42
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 mfurlend/abab788f92fc7a6adda855b9604aba23 to your computer and use it in GitHub Desktop.
Save mfurlend/abab788f92fc7a6adda855b9604aba23 to your computer and use it in GitHub Desktop.
Swap the two classes in the selector
$.fn.swapClass = function(a, b) {
if (a && b) {
this.toggleClass(a).toggleClass(b);
} else {
let arr = this.selector.split(',');
if (arr.length < 2) {
return this;
}
return this.swapClass(arr[0].substr(1), arr[1].substr(1));
}
}
//usage: $('.class-A,.class-B').swapClass()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment