I’m trying to set the CSS class “selected” at my links in my sidebar. When I am at “home” e.g. root url / I’m having trouble setting this class. Since all URL’s ends with /.
Here some relevant code:
$(document).ready(function(){
var path = location.pathname.substring();
$('nav#sitenavigation a[href$="' + path + '"]').attr('class', 'selected');
});
How can I set “selected” to ONLY the a-tag containing: href=”/” and not for example href=”/events/” ?
Use only
=(attribute equals) instead of$=(attribute ends-with), like this: