1

I am using php includes to build a mutipage website. Using jquery, how would I add a class "active" to each webpage the menu highlight whatever webpage the user is currently looking at.

This is my nav html:

<nav class="menu">
    <ul class="nav">
        <li><?php if ($thisPage=="Home") 
        echo " id=\"currentpage\""; ?><a href="http://urcsc170.org/pdouge/project3xc/index.php">Home</a></li>
        <li><?php if ($thisPage=="Jeff Bezos") 
        echo " id=\"currentpage\""; ?><a href="http://urcsc170.org/pdouge/project3xc/jeff-bezos.php">Jeff Bezos</a></li>
        <li><?php if ($thisPage=="Larry Page") 
        echo " id=\"currentpage\""; ?><a href="http://urcsc170.org/pdouge/project3xc/larry-page.php">Larry Page</a></li>
        <li><?php if ($thisPage=="Edward Snowden") 
        echo " id=\"currentpage\""; ?><a href="http://urcsc170.org/pdouge/project3xc/edward-snowden.php">Edward Snowden</a></li>
        <li><?php if ($thisPage=="Elon Musk") 
        echo " id=\"currentpage\""; ?><a href="http://urcsc170.org/pdouge/project3xc/elon-musk.php">Elon Musk</a></li>
    </ul>
</nav>
rrk
  • 15,677
  • 4
  • 29
  • 45
PatriceD
  • 13
  • 2
  • This will help http://stackoverflow.com/questions/10593591/jquery-active-class-to-menu-item-based-on-current-url – Aman Rawat Apr 29 '16 at 05:40

2 Answers2

1

Below activeurl gets current url , if it matches your active class assign automatically.

    /*active menu class*/  
 <ul class="menu">            
                <li><a  href="demo.com">home</a>
                </li>      
                <li><a href="xyz.com/category/test">Editorial</a></li>
</ul>
         $(document).ready(function(){
             var activeurl = window.location;
             $('a[href="' + activeurl + '"]').parent('li').addClass('active'); 
         });
shashik493
  • 790
  • 1
  • 10
  • 12
0

Add this to the li tag

class="<?php if ($thisPage=="Home") print $active_class; ?>"

where $active_class = active initialize this somewhere in the page

etee
  • 86
  • 6