Jump to content

navigation active class


Nic727

Recommended Posts

Hi,

 

On my website I added an active class to my main navigation for the page we are currently viewing (change css styles). The thing is that I change the class position to another link (ul > li) manually for each page. But is it possible to do that with javascript?

 

I know that active class are not really used, but I would like to know. I saw an example using Jquery, but they put active class on "home" first and it change when you click on another section. The thing is does it change if your first page you are viewing is not the home page?

 

Thank you

Link to comment
Share on other sites

If the menu is manually entered in each page the active class is added to menu to match current page, OR with JavaScript/jQuery you can loop through each menu anchor link href attribute to compare with the current url as shown in address bar and if they match add class active.

 

If the menu is added dynamically using server-side like php, where you have menu page that only contains the menu that is included in each page, you can use php or JavaScript/jQuery to compare url just like the method mentioned before.

 

Note: server-side will always show active class to match current url, while JavaScript/jQuery method relies on JavaScript being enabled.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...