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

100
Views
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 answers
Answer question

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