Search code examples
javascriptpythondjangoe-commercecart

django cart.js loop is not running


cart.js this is not working nothing is printed in the console or some declaration is wrong answer quickly

var updateBtns = document.getElementsByClassName('update-cart')

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

    })
}    

is there any mistake in html code

 {% for product in products %}
    {% if product.category == "Dosa" %}
    <div class="product">        
    <tr>
      <td><h5>{{ product.name }}</h5></td>
      <td><h5>{{ product.price }}</h5></td>
      <td><button data-product={{product.id}} data-action="add" class="btn btn-warning btn-add update-cart">Add to cart</button></td>
    </tr>
    </div>
    {% endif %}
    {% endfor %}

Solution

  • enter image description here

    Seems to be working perfectly. I would suggest to please check once in incognito/Private mode. Might be a problem of cache...