Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
How do I remove the class from the navbar button when a new link is selected [Javascript]

I am trying to make an active state on my link when clicked, which remains until another link is clicked. I'd like to have the class removed and transferred to the next link when clicked.

Currently it can be toggled on and off on each link.

// navbar toggle for active state
document.getElementById('navbar__list').addEventListener("click", function(event){
  event.target.classList.toggle('funcElemStyle');
});
.funcElemStyle {
  background: #333;
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Instead of using javascript to achive this functionality, you can use CSS to achieve the same effect by using :active.

for example:

/* Selects the link element. */
a:active {
    color: #4444AA;
}

you can also use :hover if you want effects when hovered. CSS also allows chaining so if you want an effect to apply when it is hovered over and active, you can use :active:hover

about 4 years ago · Juan Pablo Isaza Report

0

Instead of toggle use .add() and .remove() functions. Run snippet for an example.

document.querySelector("div").addEventListener("click", event=>{
  event.target.classList.remove("red")
})
div{
  width: 100px;
  height: 100px;
  background: green;
}
.red{
  background: red;
}
<div class="red"></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!