1

I want to Add a class in these list items but the condition is if I add a (active) class in a particular list item

let getList=document.querySelectorAll("li")


getList.forEach(li=>{

    li.addEventListener("click", function(){
    
      li.classList.add("active")
    
    })
})
.list-item{
  height:80px;
  width:70px;
  background-color:black
}

.list-item li{
  color:white
}
.active{
  border:5px soild blue;
  list-style:none; 
  color:orange;
  
}
<body>
<ul class="list-item">
  
  <li>Apple</li>
  <li>Banana</li>
  <li>Orange</li>
  
</ul>


</body>

The previous active class in list tag must be removed automatically

2
  • "The previous active class in list tag must be removed automatically" Can you share your previous attempt at achieving this, along with where specifically in that attempt you're getting stuck, so that the community might be able to point out where you've gone wrong? Commented Aug 20, 2020 at 15:06
  • You can also find more information on this post: stackoverflow.com/questions/63485090/… Commented Aug 20, 2020 at 15:08

5 Answers 5

7

You just need to remove the "active" class from any <li> element that already has it set. Otherwise you end up with the "active" class on multiple elements. Here is one way to do that:

let getList=document.querySelectorAll("li")


getList.forEach(li=>{

    li.addEventListener("click", function(){
    
      // remove class from any currently active elements
      getList.forEach(li => { li.classList.remove("active"); });
    
      // then add the active class to the selected element
      li.classList.add("active")
    
    })
})
.list-item{
  height:80px;
  width:70px;
  background-color:black
}

.list-item li{
  color:white
}
.active{
  border:5px soild blue;
  list-style:none; 
  color:orange;
  
}
<body>
<ul class="list-item">
  
  <li>Apple</li>
  <li>Banana</li>
  <li>Orange</li>
  
</ul>


</body>

Sign up to request clarification or add additional context in comments.

Comments

1

This is the sort of thing that is easy in jquery.

    $('ul.list-item li').on('click',function(){
      $('ul.list-item li').removeClass('active');
      $(this).addClass('active')
    }

This is nice because the selector format 'ul.list-item li' specifically excludes any other 'li' items on your page.

Without jquery you can still do this. In your function, loop through all the other li elements again and this time remove the active class.

1 Comment

its good way too..but I want to it with vanilla javascript
0

Delegation is cleaner

const list = document.querySelectorAll(".list-item li");
document.querySelector(".list-item").addEventListener("click", function(e) {
  const tgt = e.target;
  if (tgt.tagName === "LI") {
    list.forEach(li => li.classList.remove("active"));
    tgt.classList.add("active");
  }
});
.list-item {
  height: 80px;
  width: 70px;
  background-color: black
}

.list-item li {
  color: white
}

.active {
  border: 5px soild blue;
  list-style: none;
  color: orange;
}
<ul class="list-item">
  <li>Apple</li>
  <li>Banana</li>
  <li>Orange</li>
</ul>

Comments

0

you should use the onclick event and proceed like this ->

  1. Remove every active class from the <li></li> in the <ul></ul>
  2. Add the active class to the target element

Comments

0

Update the script like this.

let getList = document.querySelectorAll('li');
let prevLi = null;
getList.forEach(li => {
    li.addEventListener('click', function () {
        if (prevLi != null) {
            prevLi.classList.remove('active');
        }
        li.classList.add('active');
        prevLi = li;
    });
});
.list-item{
  height:80px;
  width:70px;
  background-color:black
}

.list-item li{
  color:white;
}
.active{
  border:5px soild blue;
  list-style:none; 
  color:orange !important;
  
}
<ul class="list-item">
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.