Jump to content

Dynamically set menu item's CSS active state


ultrasound0000

Recommended Posts

I'm implementing PHP header & footer includes throughout my website.  However some items in my header include file will change their CSS state/class depending on what section/page the visitor is in. 

 

For example, the header file includes my site's main navigation menu among other things.  Let's say the navigation menu has 5 items total and each of the menu items has an active state when a user clicks to visit that particular section.  This active state is set through CSS, something like:

 

<ul class="nav">
    <li><a href="../menu1stuff/" class="menu1">Menu One</a></li>
    <li class="active"><a href="../menu2stuff/" class="menu2">Menu Two</a></li>
    <li><a href="../menu3stuff/" class="menu3">Menu Three</a></li>
    <li><a href="../menu4stuff/" class="printnow">Menu Four</a></li>  
    <li><a href="../menu5stuff/" class="menu5">Menu Five</a></li>              
</ul>

 

Notice the active class on the <li> tag. 

 

How can I dynamically set the active class when using the header file so if visitors is on Menu One than Menu One is set to active, if on Menu Two than Menu Two is set to active and so on ... 

 

I'm trying to figure out an efficient way to accomplish this.  Any suggestions greatly appreciated.  I can use JavaScript and PHP.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.