I'm currently working on a JavaScript app project and I'm looking to add classes to my navigation links using the event.target
property. I have two different functions that should toggle the class names between the two navigation links. For example, when the user clicks on the "home" link, I want to give it the .active
class, and if they click on "home2", I want to remove the .active
class from "home" and apply it to "home2". I am seeking a Vanilla JavaScript solution for this issue. Any ideas?
function goHome(event) {
event.target.classList.add('active')
}
function goSomeWhere(event) {
event.target.classList.add('active')
}
.active {
color: red;
}
<div class="menu">
<li onclick="goHome(event)" class="primary">home</li>
<li onclick="goSomeWhere(event)" class="primary2">home2</li>
</div>