Skip to content

Instantly share code, notes, and snippets.

@stnight
Created July 31, 2020 14:08
Show Gist options
  • Save stnight/db352fa274eff10467e445853b470e75 to your computer and use it in GitHub Desktop.
Save stnight/db352fa274eff10467e445853b470e75 to your computer and use it in GitHub Desktop.
(() => {
// all add to cart button
let allAddToCartButtons = document.querySelectorAll(".addToCartButton");
let allIncreaseQtyButtons = document.querySelectorAll(".increaseQuantityButton");
let allDecreaseQtyButtons = document.querySelectorAll(".decreaseQuantityButton");
// register events on each
// for add to cart
for (let addToCartButton of allAddToCartButtons) {
addToCartButton.addEventListener('click', () => {
console.log('Clicked', addToCartButton.dataset.price);
});
}
// for increase
for (let increaseQtyButton of allIncreaseQtyButtons) {
increaseQtyButton.addEventListener('click', () => {
let nearbyInput = increaseQtyButton.parentElement.querySelector('input[type=text]');
let qty = parseInt(nearbyInput.value);
nearbyInput.value = qty + 1;
})
}
// for decrease
for (let decreaseQtyButton of allDecreaseQtyButtons) {
decreaseQtyButton.addEventListener('click', () => {
let nearbyInput = decreaseQtyButton.parentElement.querySelector('input[type=text]');
let qty = parseInt(nearbyInput.value);
nearbyInput.value = (qty !== 0) ? qty - 1 : 0;
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment