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

103
Views
How to remove a class form from the previous and the next sibling label of the currently active label with javascript

I have a function to add a class civil-active to the label that is clicked. When the next label is clicked the class is added to it too, naturally. But I want the class civil-active to be there only on the clicked label and get it removed from its previous and next sibling if present.

the code that is adding the class is

js

var labels = document.getElementsByTagName("label");

for (let i = 0; i < labels.length; i++) {
  labels[i].addEventListener("click", addclass);
}

function addclass(event) {
  event.target.classList.add("civil-active");
}

Please suggest me what should I do

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In the event handler, find the element having the class and remove it.

function addclass(event) {

  // Those lines will remove the class on the element that has it (if there is one)
  let active = document.querySelector(".civil-active")
  if(active){
    active.classList.remove("civil-active");
  }
  event.target.classList.add("civil-active");
}
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!