I've been working on implementing an add to cart feature using a combination of Javascript and Django, with the main functionality relying on Javascript.
Here's the snippet of code I have for 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)
})
}
Additionally, here is the code snippet for the index.html template:
{% 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 %}
Although print(product.id)
successfully retrieves the specific product id, the button is not functioning as intended. Can you help identify what might be the issue with my code?