javascript - Automatically Add Class To Subchild & Parent LI With jQuery -
i have been searching hours solution problem , none of them seem work way like. need jquery automatically add class "active" main li , child li if on page.
first code block simple structure, second code block how needs if on "billing_forecast.php" page.
<aside id="sidebar"> <!--| main menu |--> <ul id="side-menu" class="side-menu"> <li> <a href="index.php"> <i class="zmdi zmdi-home"></i> <span>home</span> </a> </li> <li class="sm-sub sms-bottom"> <a href=""> <i class="zmdi zmdi-money zmdi-hc-fw"></i> <span>billing</span> </a> <ul> <li><a href="billing_stats.php">statistics</a></li> <li><a href="billing_forecast.php">revenue estimator</a></li> </ul> </li> </ul> </aside>
<aside id="sidebar"> <!--| main menu |--> <ul id="side-menu" class="side-menu"> <li> <a href="index.php"> <i class="zmdi zmdi-home"></i> <span>home</span> </a> </li> <li class="active sm-sub sms-bottom"> <a href=""> <i class="zmdi zmdi-money zmdi-hc-fw"></i> <span>billing</span> </a> <ul> <li><a href="billing_stats.php">statistics</a></li> <li class="active"><a href="billing_forecast.php">revenue estimator</a></li> </ul> </li> </ul> </aside>
here's example came with:
<ul id="side-menu" class="side-menu"> <li> <ul> <li><a href="/test/">link</a></li> </ul> </li> </ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script type="text/javascript"> $(function(){ var path_name = window.location.pathname; if( $('.side-menu a[href="'+path_name+'"]').length ){ $('.side-menu a[href="'+path_name+'"]').parent('li').addclass('active'); $('.side-menu a[href="'+path_name+'"]').parent('li').parent('ul').parent('li').addclass('active') } }); </script>
Comments
Post a Comment