this is in reference to this other thread as well: jQuery – If URL matches href then move LI to top of UL?
Jquery code:
//set var to current url
var url = window.location.toString();
//if url matches li's anchor then bring it to the top of the list
$('.nav li a').each(function(){
var myHref= $(this).attr('href');
if( url == myHref) {
$(this).closest("li").prependTo(".nav");
}
});
The HTML is just from a WordPress Navigation so something like:
<navrole="navigation">
<ul>
<li>
<a href="">
The navigation points to categories, and the url matches the href’s fine and the code works. But, I cannot figure out how to get the code to also fire when I am in a child page of a category, for example when the url is:
domain.com/category-name/child-page
If I can get the Jquery to only detect the first pathname past the domain, then I will be golden. Anything past the first pathname could be omitted or setup as a wild card or something.
Any ideas?
You can try using
attribute contains selector: