'How can I change this button background color of the specific button that i have clicked? using javascript

I want to change the button's background color of the specific button that I have clicked, do I need to use loops and also condition? I tried to access the first index but I don't know how can I change the other button's background color.

let buttons = document.querySelectorAll('.btn');

let arraySelected = [];

buttons.forEach(button => {
  button.addEventListener('click', seatFunction);
});


function seatFunction(e) {

  const {
    value
  } = e.target;
  let newArray = arraySelected;
  const index = arraySelected.findIndex((element) => element === value);

  let findValue = arraySelected.find((element) => element == value);

  if (index !== -1) {
    console.log(`The ${value} has been removed into the array.`);
    // console.log(`The ${index} is the '${value}' of the item that has been removed.`);    
    newArray.splice(index, 1);
    console.log(newArray);
    buttons[0].style.backgroundColor = null;

  } else {
    console.log(`${value} has been pushed into the array.`)
    arraySelected.push(value);
    console.log(arraySelected);
    buttons[0].style.backgroundColor = "red";

  }

  // Checking if the array is empty.
  if (arraySelected.length === 0) {
    alert(`The last value '${value}' has been removed\r\nThe array now is empty!`);

  }
}
<div class="wrapper">
  <button class="btn seat" value="A">A</button>
  <button class="btn seat" value="B">B</button>
  <button class="btn seat" value="C">C</button>
  <button class="btn seat" value="D">D</button>
  <button class="btn seat" value="E">E</button>
  <button class="btn seat" value="F">F</button>
</div>


Solution 1:[1]

You're approaching it the right way. You just need to loop over all the buttons and reset their background, and then change the background of the button you clicked on.

I'm using classList and CSS for this example.

const buttons = document.querySelectorAll('.btn');

buttons.forEach(button => {
  button.addEventListener('click', seatFunction, false);
});

function seatFunction() {
  buttons.forEach(btn => btn.classList.remove('active'));
  this.classList.add('active');
}
.active { background-color: Tomato; }
.btn:hover { cursor: pointer; }
<div class="wrapper">
  <button class="btn seat" value="A">A</button>
  <button class="btn seat" value="B">B</button>
  <button class="btn seat" value="C">C</button>
  <button class="btn seat" value="D">D</button>
  <button class="btn seat" value="E">E</button>
  <button class="btn seat" value="F">F</button>
</div>

Solution 2:[2]

Use loop to remove bgcolor from other buttons then add it to clicked button.

let buttons = document.querySelectorAll('.btn');

let arraySelected = [];

buttons.forEach(button => {
  button.addEventListener('click', function() {
    seatFunction(button)
  });
});

function seatFunction(a) {
  buttons.forEach(button => {
    button.style.background = "none";
  });

  a.style.background = "red";
}
<div class="wrapper">
  <button class="btn seat" value="A">A</button>
  <button class="btn seat" value="B">B</button>
  <button class="btn seat" value="C">C</button>
  <button class="btn seat" value="D">D</button>
  <button class="btn seat" value="E">E</button>
  <button class="btn seat" value="F">F</button>
</div>

Solution 3:[3]

I think you do not need all of that (loop and condition), and you do not need to create another array.

First, I advise you to work with CSS class in order to change any style, even dynamically:

.active {
    background-color: #00d4b1;
}

So now the logic is when a button is clicked "if there is the active class on it, remove the active class, else attach the active class."

function seatFunction(e){
    e.target.classList.toggle("active");
}

The beginning of your script is good:

let buttons = document.querySelectorAll('.btn');

buttons.forEach(button => {
    button.addEventListener('click', seatFunction);
});

Solution 4:[4]

Why don't you install jquery, you can achieve this easily.

just put this in your html header and try

<script src="https://code.jquery.com/jquery-3.6.0.js"></script>

In your JS, put like this

$('.btn').click(function(){
   $('.btn').css({"background":""});
   $(this).css({"background":"red"}); //put the color you want
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andy
Solution 2 Abhishek Pandey
Solution 3 Dylan Barquilla
Solution 4 Gerard Haw