views:

30

answers:

1

Hi folks, i have a menu, and i need the link for the current page i am on to be highlighted differently. I do a really twisted way of comparing strings now, but i am sure there is a smarter way?

  jQuery(document).ready (function(){
        jQuery(".filteroptions .option").each (function (index,ele) 
        {
                link=jQuery(ele).find("a").attr("href").toString();
                p=jQuery(window).attr("location").toString().search(link)
                if (p!=-1){
                        jQuery(ele).find(".link").addClass("current")
                }
        });
  });
+1  A: 

Something like this would work...

$('a').each(function(){
    if ( $(this).attr('href') == window.location.href ) {
        $(this).addClass('current');
    }
});

Or if you're not using full-path URLs in your links...

$('a').each(function(){
    if ( window.location.href.indexOf( $(this).attr('href') ) !== -1 ) {
        $(this).addClass('current');
    }
});
BBonifield