add class to highlight navigation item if page url


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



Copy this code and paste it in your HTML
  1. <script type="text/javascript">
  2. $(function(){
  3. $page = location.pathname;
  4. if(!$page) {
  5. $page = 'index.html';
  6. }
  7. $('ul.navigation li a').each(function(){
  8. var $href = $(this).attr('href');
  9. if ( ($href == $page) || ($href == '') ) {
  10. $(this).addClass('on');
  11. } else {
  12. $(this).removeClass('on');
  13. }
  14. });
  15. });
  16. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.