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>
1

2 Answers 2

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'); 
         });
Sign up to request clarification or add additional context in comments.

2 Comments

Or in the head of the pages
you add below the code or bottom of the page .Code runs once the page is loaded . Because we using document.ready function.
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

2 Comments

Should I add this to every li tag?
As per my solution yes, you have to add this to every li tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.