Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jQuery to match current URL to herf in a UL and add class if matched

Tags:

jquery

I have a dynamically created UL. I want to use jQuery to use the current URL, search through a specified UL and find any li with a matching href. If one is found then add the class "current_page".

I came up with this but it doesn't seem to do the trick.

$(document).ready(function() {  
    $("ul.nav_cat_archiveli").find("a[href='"+window.location.pathname+"']").each(function() {
        $(this).addClass("current_page");
    });
});
like image 247
Dan Avatar asked Nov 15 '22 09:11

Dan


1 Answers

You can play with window.location which will return the current page URL and also the href property of the window.location

like image 135
Amr Elgarhy Avatar answered Jan 02 '23 19:01

Amr Elgarhy