Set links with href to current page as active (or if in subnav set their parent to active)


/ Published in: JavaScript
Save to your folder(s)

look through the list items in your main nav and based on href of anchor tags this will set the top level link to have an 'active' class.


Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3. $(".sf-menu li").children("a").each(function() {
  4. if(window.location.href == $(this).attr('href')){
  5. if($(this).parent().hasClass('main_link')){
  6. $(this).addClass('active');
  7. }else{
  8. var topLink = $(this).parents('.main_link');
  9. topLink.find('a').first().addClass('active')
  10. }
  11. }
  12. });
  13. });
  14. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.