this follow on previous question. want achieve add class li element after clicking anchor tag render new page. understand when page reloads new html loaded , addclass method forgotten.
so have been looking , seems if there still way jquery? hoping perhaps clarify or let me know wrong following
so have.....
navbar
<ul id="main-menu" class="clearfix sf-js-enabled sf-arrows"> <li> <a href="/"><i class="icon-home"></i> home</a> </li> <li> <a href="/pages/aboutme"><i class="icon-desktop"></i> me</a> </li> <li> <a href="/pages/portfolio"><i class="icon-picture"></i> portfolio</a> </li> <li> <a href="/blog"><i class="icon-comments"></i> blog</a> </li> <li> <a href="/contact"><i class="icon-envelope"></i> contact</a> </li> </ul> jquery
$('#main-menu li').each(function() { var href = $(this).find('a').attr('href'); if (href === window.location.pathname) { $(this).addclass('current-menu-item'); } }); though isnt working. please explain why? or see error in code, initial thoughts need find closest current li ?
any appreciated
thanks
just put 1 line after href variable.
console.log(href) you understand going wrong there. need see in console. if not aware use following.
alert(href)
Comments
Post a Comment