I can't add more than one data to array. I couldn't solve this problem running on basic javascript events.
$(".add-cart").on("click", function () {
let products = []
let name, price
name = "product 1"
price = "200"
products.push({
name: name,
price: price
})
// i will click so many times and result is only one length array
// products
// [{name: 'product 1', price: 200}]
});
CodePudding user response:
You need to define that array outside of your event. So every time you click it uses the same reference to add your data.
let products = []
$(".add-cart").on("click", function () {
let name, price
name = "product 1"
price = "200"
products.push({
name: name,
price: price
})
// i will click so many times and result is only one length array
// products
// [{name: 'product 1', price: 200}]
});