I have a menu but I want to highlight current link with jquery.
var loc = window.location;
var lochref = $("#topNavigation li a").attr("href");
if(lochref == loc){
$('#topNavigation li a').addClass('currenthover');
}
With above script, there is no any change in “a” class. How can I do this with jquery?
Thanks in advance
First off: This is something you should consider doing server side. It’s much, much, much more simpler and reliable.
Your code compares only the href of the first link, as
attrreturns the property value of the first link. You need to loop over the links to find the right one.