Skip to content

Instantly share code, notes, and snippets.

@darren-rose
Created July 31, 2014 21:04
Show Gist options
  • Save darren-rose/b150a17879adba89993c to your computer and use it in GitHub Desktop.
Save darren-rose/b150a17879adba89993c to your computer and use it in GitHub Desktop.
jQuery plugin to add an click event listener that adds a class and removes the class from all other elements
<script>
(function ( $ ) {
$.onClickAddClassMutuallyExclusive = function( options ) {
var settings = $.extend({
selector : "a",
excludeSelector: ".excluded",
theClass : "spinner"
}, options );
$(settings.selector).not(settings.excludeSelector).click(function(){
$(settings.selector).removeClass(settings.theClass);
$(this).addClass(settings.theClass);
console && console.log(this.id);
});
};
}( jQuery ));
</script>
@darren-rose
Copy link
Author

<script> (function ( $ ) { $.onClickAddClass = function( options ) { var settings = $.extend({ selector : "a", excludeSelector: ".excluded", theClass : "spinner", exclusive : true }, options ); $(settings.selector).not(settings.excludeSelector).click(function(){ if(settings.exclusive){ $(settings.selector).removeClass(settings.theClass); } $(this).addClass(settings.theClass); console && console.log(this.id); }); }; }( jQuery )); </script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment