Set nav bar item to active using Javascript
On a couple of occassions I’ve found myself needing to set an active selector on a nav bar item using Javascript; this post documents how this was done. I have used this technique in both a straight-forward side-bar nav and on a Bootstrap styled nav bar for a Django app.
It is simply done using a bit of Jquery and works as follows:
In the case of a simple nav bar (made of <a>
tags inside <li>
tags)
In the case of a Bootstrap navbar it is also necessary to take care of click events