Use $(this).attr("href")to get value hreffrom your links since you are already using jQuery.
Combine this with the andrew clause to get the end of the current URL, and now you have the best values to check in the instruction if.
$(function(){
var url = window.location.href.split('/');
url = url[url.length-1];
$("#menu a").each(function() {
var $this = $(this);
if (url === $this.attr("href")) {
$this.closest("li").addClass("active");
}
});
});
UPDATE:Even better would be to use advanced jQuery selectors:
$(function(){
var url = window.location.href.split('/');
url = url[url.length-1];
$("#menu a[href='" + url + "'").addClass("active");
});
source
share