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

126
Vistas
Click change color

I am working on a website and I want that when the user clicks on a specific card, it turns blue. I have searched and found some things online, but it is not working for me and I don't know why; here is the code:

<div class="card" onclick="myFunction()">
  <p class="number-card">1</p>
</div>

<script>
  function myFunction() {
    document.getElementById("card").style.backgroundcolor = "blue";
  }
</script>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

  • You're missing an ID - anyways, use Classes instead with document.querySelectorAll(".card") MDN querySelectorAll
  • Should be backgroundColor with capital C
  • Use Element.addEventListener() instead of inline JS on* attribute handlers. JS and styles should always be in their respective tags or files, not disseminated around a HTML file.
  • Read about Event.currentTarget

<div class="card">
  <p class="number-card">1</p>
</div>

<div class="card">
  <p class="number-card">2</p>
</div>

<script>
  const changeBgColor = (ev) => {
    ev.currentTarget.style.backgroundColor = "blue";
  };
  const EL_cards = document.querySelectorAll(".card");
  EL_cards.forEach(EL => EL.addEventListener("click", changeBgColor));
</script>

Alternatively, using a regular function (where addEventListener binds by default this to the Event.currentTarget Element):

function changeBgColor() {
  this.style.backgroundColor = "blue";
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You only have the class set to card but you're trying to getElementById, you should give the div an id that's unique and change the function.

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