Reputation: 25
I am trying to update the value of num inside my object each time i click a button. However I can not get it to work. I am printing the object to the console each time I click so i can see if the value updates but it doesn't. Here is my code... any help would be appreciated. Thank you!
const addToCart = document.getElementById('addToCart')
addToCart.addEventListener('click', function(option) {
option.preventDefault();
let num = 0
let cartObject = {
name: jsonResponse.name,
price: jsonResponse.price,
option: select.value,
optionQuantity: num
}
num++
console.log(cartObject)
});
Upvotes: 0
Views: 153
Reputation: 48
You have to move your let num = 0
to outside of your function block. Because when you invoke this function each time it's executes let num = 0
. That why num
never changes.
Upvotes: 1