Javascript – Dynamically adding an “active” class to the nav

In your navigation menu, you would like to dynamically add an “active” class to each menu item when you’re on the corresponding pages so you can style those menu items.

 

HTML:

 

Javascript:

  • Austin

    Hi, I tried to implement this but I think I must be missing something. Do you have a demo that I could look at?

  • Heinz

    Very good work 🙂 tank you so much

  • Lennard Berger

    haven’t got it working in Chromium 41.0.2272.76 nor in Firefox 37.0 with recent JQuery (2.3.1). Did something change?

  • Cindy

    @Lennard Berger, hard to tell what’s happening since I cannot see your code. How about trying this method instead? http://bit.ly/ninjacss

  • NinjaCSS

    Hi Lennard. Hard to tell what’s happening since I cannot see your code. How about trying this method instead? http://bit.ly/ninjacss

    Or please feel free to show me your code here.

  • al joulson

    Would be very helpful if they actually post a demo with this. Only works with links that have home.html, not working with relative paths.. for example ./home