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

182
Views
el evento de clic con JavaScript no funciona en Django

este es el código para (cart.js) en la carpeta static/js

 var updateBtns = document.getElementsByClassName('update-cart') for (i = 0; i < updateBtns.length; i++) { updateBtns[i].addEventListener('click', function(){ var productId = this.dataset.product var action = this.dataset.action console.log('productId:', productId, 'Action:', action) }) }

y en el archivo HTML en la parte inferior:

 <button data-product="{{ product.id }}" data-action="add" class="update-cart btn btn-outline-secondary add-btn ">Add to Cart</button>

y llama al js en main.html

 <script type="text/javascript" src="{% static 'js/cart.js' %}"> </script>

y agrego static en el setting.py, y todo correcto.

y todo funciona bien, cuando intento (console.log) sin un evento de clic de botón... el problema es solo con el evento de botón porque no funciona

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Su pregunta no está relacionada con Django. Parece que no utiliza JavaScript correctamente. Cuando el navegador ejecuta el código de card.js, el árbol HTML DOM aún no está construido. Para adjuntar eventos JS, debe cambiar el código cart.js a:

 function initUpdateBtns() { var updateBtns = document.getElementsByClassName('update-cart') for (i = 0; i < updateBtns.length; i++) { updateBtns[i].addEventListener('click', function(){ var productId = this.dataset.product var action = this.dataset.action console.log('productId:', productId, 'Action:', action) }) } } document.addEventListener('DOMContentLoaded', initUpdateBtns)

about 4 years ago · Santiago Trujillo 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!