Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda