Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
Cambié el nombre de la clase en el documento a través de "getElementById" pero no tiene efecto

Gracias por adelantado. Cambié el nombre de la clase en JavaScript, pero no cambia. [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'})}

Cuando verifico (document.getElementById(this.dataset.product).class agregando una alerta en la línea inferior, aparece "btn-primary", pero esto no se aplica en la página. ¿Qué debo hacer?

También probé con classList.add , pero no funcionó, así que me preguntaba si hay algo más que hacer, como devolver el documento.

Gracias.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

classtList.add('class-name') debería ser lo que necesita: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

Eso suponiendo que desea agregar btn-primary a las clases que ya están en el elemento.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!