This question is a bit outdated, but I think there is still room for improvement.
"Traditional" binding of local events:
$("ul a").click(function(){ $(this).parent("li").addClass("active") .siblings().removeClass("active");
Now the same thing, using delegation delegation through delegate () (jQuery +1.4.2). Allows you to dynamically add additional> li> a without having to replay the event:
$("ul").delegate("a", "click", function(){ $(this).parent("li").addClass("active") .siblings().removeClass("active"); # return false to cancel the default action # and prevent it from bubbling (further) up: return false; });
Change "ul" to everything that matches exclusively the list (s), for example. ".linksList", "#nav", etc.
Pablo ziliani
source share