Created
August 11, 2010 18:36
-
-
Save jch/519486 to your computer and use it in GitHub Desktop.
jquery.setCurrentClassOnEvent.js
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
// 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