first remove active class from classlist and append to current element using javascript 1

first remove active class from classlist and append to current element using javascript

function myFunction(e) {
  if (document.querySelector('#navList a.active') !== null) {
    document.querySelector('#navList a.active').classList.remove('active');
  }
  e.target.className = "active";
}

Here is what the above code is Doing:
1. We’re selecting the navList element and adding an event listener to it.
2. We’re adding a click event listener to the navList element.
3. We’re adding a function to the click event listener.
4. We’re selecting the active class and removing it from the element.
5. We’re adding the active class to the element that was clicked.

Similar Posts