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

201
Vistas
I changed the class name in document via "getElementById" but it doesn't take effect

Thank you for advance. I changed the class name in JavaScript, but it doesn't change. [html]

<button id={{ product.id }} data-product={{ product.id }} data-action='add' class="btn btn-outline-secondary add-btn store-cart"> button</button>

[javascript]

var storeBtns = document.getElementsByClassName('store-cart')

for (i = 0; i < storeBtns.length; i++) {
    storeBtns[i].addEventListener('click', function(){
        document.getElementById(this.dataset.product).class = 'btn-primary'})}

When I check (document.getElementById(this.dataset.product).class by adding alert to the bottom line, "btn-primary" appears, but this does not apply in the page. What should I do?

I also tried with classList.add , but it didn't work, so I was wondering if there is anything else to do, such as returning document .

Thank you.

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

0

classtList.add('class-name') should be what you need: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

That's assuming you want to append btn-primary to the classes already on the element.

for (i = 0; i < storeBtns.length; i++) {
    storeBtns[i].addEventListener('click', function(){
        document.getElementById(this.dataset.product).classList.add('btn-primary');
   });
}
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