Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

add active class and remove active class by click

/*
Here are some HTML codes looks like
<ul class="navbar-nav m-auto mb-2 pb-lg-0 main_menu">
              <li class="nav-item position-relative me-5">
                <a class="nav-link active" aria-current="page" href="#">Home</a>
              </li>
              <li class="nav-item me-5">
                <a class="nav-link" href="#">About</a>
              </li>
              <li class="nav-item me-5">
                <a class="nav-link" href="#">Elements</a>
              </li>
              <li class="nav-item me-5">
                <a class="nav-link" href="#">Blogs</a>
              </li>
              <li class="nav-item me-5">
                <a class="nav-link" href="#">Portfolio</a>
              </li>
              <li class="nav-item me-5">
                <a class="nav-link" href="#">Contact</a>
              </li>
              
            </ul>
*/

// Here are javascript code looks like
const links = document.querySelectorAll(".nav-link");

links.forEach((link)=> {
    link.addEventListener("click", function(){
        links.forEach((e)=> {e.classList.remove('active')})
        this.classList.add('active')
    })
})
Comment

add and remove active class

links.forEach((link)=> {
 	 /*
  		*important may you face an problem that's becuase the arow function
 		doesn't work here.
        --so dont use arow function--
 	*/
    link.addEventListener('click', function(){
        links.forEach((e)=> {e.classList.remove('active')})
        this.classList.add('active')
    })
}) 
Comment

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();
Comment

PREVIOUS NEXT
Code Example
Javascript :: get client id socket io 
Javascript :: javascript truncate decimal without rounding 
Javascript :: slide out navigation 
Javascript :: how to change color on js 
Javascript :: getcomputedstyle 
Javascript :: express req body undefined 
Javascript :: javascript get element by rel attribute 
Javascript :: javascript how to increment by other than one in for loop 
Javascript :: when i click on one checkbox check all checkboxes 
Javascript :: flask return status code 200 and json 
Javascript :: js convert date to timestamp 
Javascript :: preload javascript 
Javascript :: javascript indexof 
Javascript :: passing livewire variable in alpine js 
Javascript :: countdown in react js 
Javascript :: react.fragment react native 
Javascript :: get first word in javascript 
Javascript :: Error: Unable to resolve module ./index from 
Javascript :: $(document).ready(function() alert 
Javascript :: angular cors issue 
Javascript :: toggle boolean js 
Javascript :: javascript split sentence into words 
Javascript :: javascript decode a sting in base64 
Javascript :: urlencoded limit 
Javascript :: loading image in react js 
Javascript :: how to compare two time in moment js 
Javascript :: javascript is radio button checked 
Javascript :: unique element in array 
Javascript :: difference between let and var in javascript 
Javascript :: count the number of elements in an array javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =