You need to loop through the anchors, not the list items. The href
is a property of the anchor. Do that loop and you can then apply .addClass()
to the closest list item. Also, if you are using jQuery, you may as well use the reliable jQuery trim()
:
$("#main-nav li a").each(function(index) {
if($.trim(this.href) == window.location) {
$(this).closest('li').addClass("active");
//alert(this.href.trim());
}
});