Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

How to add active class to menu or li after form load using jQuery

If you want to make menu active using jQuery after form load use my following code. You can also use this code if you have ul/ li and want to add active class to selected li.

<ul>
  <li><a href="https://www.blogger.com/page1.php">page 1</a></li>
  <li><a href="https://www.blogger.com/page2.php">page 2</a></li>
</ul>


$('li').each(function(){
    if(window.location.href.indexOf($(this).find('a:first').attr('href'))>-1)
    {
    $(this).addClass('active').siblings().removeClass('active');
    }
});

Please support us, Like us on Facebook.

  1. Replies
    1. It is working Gokulan. Make sure there is jquery library added in your page. If not than add following javascript in your page.

      http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js

      Delete

 

Copyright @ 2017 HKBlog.