Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

328
Vistas
Changing Background Colour of Button - Change when clicked, remove when another button is clicked

First time poster - have tried to find solution elsewhere but not able to.

I have created a grid of buttons. Upon clicking button 1, I want the background to change to a preset colour. When another button is clicked (button 2), I would like the button(1) background to change back to the original, followed by the newly clicked button (button 2) to take on the preset colour.

Here is the HTML written so far:

    <div class="tip-select">
      <p class="ttext">Select Tip %</p>
      <div class="tip">
        <div class="five-tip btn tips">5%</div>
        <div class="ten-tip btn tips">10%</div>
        <div class="fifteen-tip btn active-tip tips">15%</div>
        <div class="twentyfive-tip btn tips">25%</div>
        <div class="fifty-tip btn tips">50%</div>
        <div class="custom btn tips">Custom</div>
      </div>
    </div>

Here is the Javascript written so far:

tips.forEach(function (mov) {
  mov.addEventListener("click", handleClick);
});


function handleClick(event) {
tips.forEach(function (val) {
  if (event.target.innerHTMl == val.innerHTMl) {
    val.classList.add("active-tip");
    console.log(val);
  }
  });
}

Here is the current design for reference:

Grid to change

My thought is to iterate through the nodelist and assign active-tip as a class based on the button selected. At the moment though all of the buttons are changing colour when clicked.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

First, JavaScript is case sensitive, so innerHTMl should read innerHTML. But that isn't a sound method to achieve your wanted effect.

Also, tips needs to be an array, so use querySelectorAll to return a live collection ('live' means that only references to the elements are stored and not the elements). To make this into an array, use Array.from().

remove all the active-tip styles first, and then add the new one to the required element.

tips=Array.from(document.querySelectorAll('.tips'));

tips.forEach(function (mov) {
mov.addEventListener("click", handleClick);
});


function handleClick(e) {
tips.forEach((t)=>t.classList.remove("active-tip"));
e.target.classList.add("active-tip");
}
.active-tip {color:red}
    <div class="tip-select">
      <p class="ttext">Select Tip %</p>
      <div class="tip">
        <div class="five-tip btn tips">5%</div>
        <div class="ten-tip btn tips">10%</div>
        <div class="fifteen-tip btn active-tip tips">15%</div>
        <div class="twentyfive-tip btn tips">25%</div>
        <div class="fifty-tip btn tips">50%</div>
        <div class="custom btn tips">Custom</div>
      </div>
    </div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You you can add a prameter to function which is the id or class of your div so when your function called your function take the innertext of the div with you id you get from pramerter.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I suggest you compare the node objects rather than their innerHtml. In addition, you are missing the else case to reset all non selected nodes (remove the active-tip class).

Here's an example

let tips = Array.from(document.getElementsByClassName('tips'))

tips.forEach(function (mov) {
  mov.addEventListener("click", handleClick);
});

function handleClick(event) {
  tips.forEach(function (val) {
    if (val == event.target) {
      val.classList.add("active-tip");
    } else {
      val.classList.remove("active-tip");
    }
  });
}

and the complete codepen example here https://codepen.io/beezital/pen/OJxoxmK

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda