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

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

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 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