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

96
Vistas
Javascript: Ecommerce add to cart function not working in Javascript?

I am trying to write an add to cart functionality using Javascript and django (main functionality is Javascript).

I have written this code for the cart.js

var updateBtns = document.getElementsByClassName('update-cart')
console.log("Working");

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 is:', productId, 'Action is:', action)
        console.log('USER:', user)

    })
}

And this is the code for template index.html

{% for product in products %}
{{ product.title }}
{{ product.price}}

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

When i print(product.id) it gets the specific product id but the button does not wwork what could be the problem with my code?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Did you actually call that action?

for (i = 0; i < updateBtns.length; i++) {
    updateBtns[i].addEventListener('click', function(){
        var productId = this.dataset.product
        var action = this.dataset.action

        // I think that you need to call the action
        window[action](productId);

        console.log('productId is:', productId, 'Action is:', action)
        console.log('USER:', user)

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