Estoy tratando de escribir una funcionalidad de agregar al carrito usando Javascript y django (la funcionalidad principal es Javascript).
He escrito este código para 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) }) }Y este es el código para la plantilla 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 %} Cuando print(product.id) obtiene la identificación del producto específico, pero el botón no funciona. ¿Cuál podría ser el problema con mi código?
¿Realmente llamaste a esa acción?
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) }) }