I
I
idpgg2021-06-10 17:26:03
JavaScript
idpgg, 2021-06-10 17:26:03

How to change color on click? and then remove?

<p class="car-fleet car-gazel-mobil" id="positionSlide" data-position="0" data-color="red">1</p>
  <p class="car-fleet" id="positionSlide" data-position="1060" data-color="blue" >2</p>
  <p class="car-fleet" id="positionSlide" data-position="2120" data-color="black">3</p>

document.querySelectorAll("#positionSlide").forEach((positionSlide) => {
  positionSlide.addEventListener("click", function () {
    sliderWidth.style.left = -this.dataset.position + "px"
      this.classList.add ('color') 

  });
});

there is a code, it changes the position to the number of pixels I need.
I need that when I click on my buttons, in addition to the position, I also change the borderColor of the selected button, and when I click on another button, I change its borderColor, and remove the borderColor from the previous one

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2021-06-10
@ThunderCat

querySelectorAll("#positionSlide") does not seem to be a valid query, since the id must be unique on the page.
For the rest, add another change of border color to the function, first for everyone to non, and then for the current one to the desired border.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question