> // 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.

Reply via email to