I recently ran into a method in the Ext javascript library called radioClass. This method will add one or more CSS classes to an element and remove the class(es) from the siblings of the element. I found that this would be helpful in jQuery since I find myself doing something similar often:
$("li").addClass("selected").siblings().removeClass("selected");
I wrote the following extension method in jQuery to reduce the amount of code.
Code:
$.fn.extend({
radioClass: function(c) {
return $(this).addClass(c).siblings().removeClass(c).end();
}
});
Usage:
$("li").radioClass("selected");
September 3, 2009 at 3:08 pm
[...] out the blog post on the Lanit Development Blog – jQuery Extension: radioClass() method This entry was posted on Thursday, September 3rd, 2009 at 8:08 am. You can follow any responses [...]