'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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source