Hello everyone, this is my first time posting here and I'm still trying to figure things out, so I appreciate your patience.
Currently, I am experimenting with vanilla JS to create a functionality where clicked anchors are highlighted while removing the highlight from previously selected anchors.
I'm encountering some issues with this feature and as a beginner, I'm still learning the ropes.
Below is the snippet of JS code related to this task. The links have a class of .nav-link and 'active' is supposed to manipulate them after being clicked.
window.onload = afterClick;
function afterClick(){
let linkClass = document.querySelectorAll(".nav-link");
linkClass.forEach(link => link.addEventListener("click", function(){
if (link.classList.contains("active")) {
link.classList.remove("active");
}
link.classList.add("active");
}));
}
Thank you for your help and understanding!