I am trying to decrease the quantity by 1 from an object, I have created a function in the object called sell()
, every time I am trying to call the function when a button is pressed I am not receiving any answer, this.quantity
is not going down, I would need to find out how to call that function from outside the object.
class item {
constructor(name, price, quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
//this is the function i am trying to activate
sell(e) {
e.preventDefault();
return `${this.name} ${this.price} ${this.quantity-1}`
}
store(count) {
return `${this.name} ${this.price} ${this.quantity+count}`
}
}
const items = [
new item('soap', 11, 3),
new item('bread', 12, 2),
new item('apples', 13, 5),
new item('banana', 15, 7),
new item('grappe', 16, 5),
new item('water', 9, 7),
new item('beer', 4, 9),
];
for (let i = 0; i < items.length; i++) {
const list = document.querySelector('.elements');
const newLi = document.createElement('tr');
newLi.innerHTML += `
<td>${items[i].name}</td><td>${items[i].price}</td><td>${items[i].quantity}</td><button class="buyer">Buy</buy>`;
list.appendChild(newLi);
let btnBuy = newLi.querySelector('.buyer');
//this is the listener that should activate sell(),
btnBuy.addEventListener('click', sell);
}
<table class="elements">
<thead>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
</thead>
<tbody></tbody>
</table>
thanks to everyone I hope you can help