Skip to content

Instantly share code, notes, and snippets.

@jch
Created August 11, 2010 18:36
Show Gist options
  • Save jch/519486 to your computer and use it in GitHub Desktop.
Save jch/519486 to your computer and use it in GitHub Desktop.
jquery.setCurrentClassOnEvent.js
// For a group of elements, sets 'className' on an element when 'evt'
// is triggered, and removes 'className' from all it's
// siblings. Useful for highlighting a selection within a list of
// choices.
//
// $('ul#my_list li').setCurrentClassOnEvent('click', 'current');
// <ul id="my_list">
// <li class="current">Chocolate</li> <!-- click adds 'current' class -->
// <li>Vanilla<li>
// <li>Strawberry</li>
// </ul>
jQuery.fn.setCurrentClassOnEvent = function(evt, className) {
if (className === undefined) {
className = 'current';
}
this.live(evt, function() {
var that = $(this);
that.siblings().removeClass(className);
that.addClass(className);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment