I have a navbar that looks like this: https://i.sstatic.net/1jcRr.png
Below is the relevant HTML code for the navbar:
<div class = "navbar">
<a class = "active" href ="{% url 'keiji-home' %}">home</a>
<a href ="#" onclick = "makeActiveLink('communities-tag')"
id="communities-tag">
communities
</a>
<a href = "#">feeling lucky</a>
<a href = "{% url 'keiji-about' %}">about</a>
<div class = "navbar--right">
<a href = "{% url 'register' %}">login/signup</a>
</div>
</div>
My goal is to have a red background (class = "active") on the link (such as "home", "communities", "feeling lucky", etc.) when I am on that specific page. For example, if I'm on the "about" page, the red background should be on the about link.
As someone who is new to JavaScript, here is what I've attempted so far after researching other questions:
function makeActiveLink(id)
{
var selectedLink = document.getElementById(id);
selectedLink.classList.className += "active";
}
And then on the link, for instance, "communities":
<a href ="#" onclick = "makeActiveLink('communities-tag')"
id="communities-tag">
However, my current approach is not yielding the desired result.
In addition, I would like the "active" class to be removed from the previous link. In other words, when transitioning from the "home" page to the "about" page, the class should be removed from "home" and added to "about".
Although I'm unsure if this information is relevant, I am using Django (which I'm also new to) to operate this website.