*chuckle* As Dave has shown, sometimes it's good to rebuild code "from scratch" rather than trying to optimise existing code (which is what I did). Well done Dave.
Karl Rudd On 10/11/07, Dave Methvin <[EMAIL PROTECTED]> wrote: > > > // save old active menu > > var oldActive = $("#mainnav li.active"); > > > > // clear actives > > $("#mainnav li.active").removeClass("active"); > > > > // activate current > > $("#mainnav li a").filter(function() { > > return isCurrent(this, 'href'); > > > > }).parent().addClass("active"); > > > > // re-activate old one if nothing is active > > if($("#mainnav li.active").length == 0) { > > oldActive.addClass("active"); > > > > } > > Maybe something like this (untested)? > > $("#mainnav li a") > .filter(function() { return isCurrent(this, 'href'); }) > .parent() > .siblings(".active").removeClass("active").end() > .addClass("active") > > I think it handles the same two cases as the original code. > > 1) The filter returns a node matching href; we get the parent li and > from there the current active sibling. Then we remove the active class > from it and end() back to the li matching the href. That one gets the > active class. > > 2) The filter matches no href; the matched node set in the jQuery > object is empty and the rest of the chain is a no-op. Since nothing is > changed, the original active node retains its class. > >