As already written in title, I want to delete active class in parent and underlined class in child when new list item is clicked and add these two classes on clicked item.
With the code written, I manage to remove the active and underline from the first element (the first element has class active and its child the class underline by default) when I click on a new link, but it stays fixed on the clicked links, while I want delete these two classes and activate them again.
So I want them to be active only for every link I click.
This is my code:
function sectclick(){
let section=document.querySelectorAll(".ulmargin li");
for (let i = 0; i < section.length; i++) {
section[i].addEventListener("click", function() {
if( i != 0 ){
section[0].classList.remove("active");
section[0].children[0].classList.remove("underline");
section[i].classList.add("active");
section[i].children[0].classList.add("underline");
}
})
}
}
<ul class="ulmargin">
<li class="active"><a class="underline" href="#">Home</a></li>
<li><a href="#divabout">About</a></li>
<li><a href="#divservices">Services</a></li>
<li><a href="#divskills">Skills</a></li>
<li><a href="#diveducation">Education</a></li>
<li><a href="#divexperience">Experience</a></li>
<li><a href="#divwork">Work</a></li>
<li><a href="#divblog">Blog</a></li>
<li><a href="#divcontact">Contact</a></li>
</ul>
Can anyone help me fix the code to get what I want? I'm new to javascript.