Skip to content

Instantly share code, notes, and snippets.

@dmhendricks
Last active May 13, 2018 20:30
Show Gist options
  • Save dmhendricks/ec6a833ce9980a1e78236824756358cb to your computer and use it in GitHub Desktop.
Save dmhendricks/ec6a833ce9980a1e78236824756358cb to your computer and use it in GitHub Desktop.
jQuery function to replace one or more CSS classes with others.
/**
* jQuery function to replace one or more CSS classes with others. Separate multiple
* classes with spaces.
* @param {string} remove - CSS classes to remove.
* @param {string} add - CSS classes to add.
* @example
* $( '.selector' ).replaceClass( 'old-class', 'new-class some-other-class' );
* $( '.selector' ).replaceClass( 'old-class another-class', 'new-class' );
*/
!function(s){s.fn.replaceClass=function(remove,add){var e=add||remove;return e&&this.removeClass(add?remove:this.className).addClass(e),this}}(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment