Created
July 31, 2014 21:04
-
-
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
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
<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> |
Author
darren-rose
commented
Jul 31, 2014
<script>
$(document).ready(function(){
console && console.log("ready");
$.onClickAddClassMutuallyExclusive({selector : "a", excludeSelector : ".excluded", theClass : "spinner"});
});
</script>
<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