0

I am having a button when I click it becomes active but on another click, I want to remove active CSS

When I click on the button it becomes active by adding an active class to it. Here is what I tried, but what I was trying is when I click on the same buttons the active CSS should be removed, basically want to do toggle

const myDemo = (event) => {
  const clickedElem = event.target
  const allBtns = document.querySelectorAll('.btn.light')
  allBtns.forEach(btn => btn.classList.remove('active'))
  clickedElem.classList.add('active')
}
.active {
  background: red;
}
<button onclick="myDemo(event)" class="btn light">DOG</button>

4 Answers 4

1

You can use clickedElem.classList.toggle to toggle the classes.

Before that, you need to remove the active class names except for the current selected button as follows.

const myDemo = (event) => {
  const clickedElem = event.target;
  console.log(clickedElem);
  const allBtns = document.querySelectorAll('.btn.light');
  allBtns.forEach(btn => {
    if (clickedElem != btn) {
      btn.classList.remove('active');
    }
  });
  clickedElem.classList.toggle('active');
}
.active {
  background: red;
}
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>

Or the other way, you can store the previous selected button and use it.

let previousClicked = null;

const myDemo = (event) => {
  const clickedElem = event.target;
  if (clickedElem === previousClicked) {
    clickedElem.classList.remove('active');
    return;
  }
  
  if (previousClicked != null) {
    previousClicked.classList.remove('active');
  }
  clickedElem.classList.add('active');
  previousClicked = clickedElem;
}
.active {
  background: red;
}
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>

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

1 Comment

Thanks, @Derek.W, I was looking for a multi-select option
0

You can try using built-in toggle function in your "myDemo" function. Instead of using remove(), just add this line:

btn.classList.toggle("active");

Comments

0

I suggest using classList.toggle().

const myDemo = (event) => {
  const clickedElem = event.target;
  const allBtns = document.querySelectorAll('.btn.light');

  // remove the class from all buttons
  allBtns.forEach(btn => btn.classList.remove('active'));

  // reapply the class for the button that was clicked
  clickedElem.classList.toggle('active');
}
.active {
  background: red;
}
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>
<button onclick="myDemo(event)" class="btn light">DOG</button>

Comments

0

From what I can see in your function, you seem to want to also remove the active class from any other button else in the page. This is my approach:

let activeButton;

const selectButton = e => {
  if (activeButton) {
    activeButton.classList.remove('active');
  }

  if (activeButton === e.currentTarget) {
    activeButton = null;
    return;
  }

  e.currentTarget.classList.add('active');
  activeButton = e.currentTarget;
};

This way it does not have to loop though every buttons to deactive them, as it keeps track of the active button.

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.