Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Add and remove active class

const ul_li = document.querySelectorAll('.list-item');
function add_and_remove_active_class() {
  //for in will not work with DOM elements  
  for (const elem of ul_li) {
      elem.addEventListener('click' , ()=>{
        for (const lis of ul_li) {
          lis.classList.remove('active-item');
        }
        elem.classList.add('active-item');
      });
    }
}
add_and_remove_active_class();
 
PREVIOUS NEXT
Tagged: #Add #remove #active #class
ADD COMMENT
Topic
Name
8+9 =