I dont know what I am doing wrong, but I cant push object to an existing object. I have boxes, and calculator, ... after click on box \".items\"
...
Well actually its not inserting last one, its inserting the same item every time. The reason is the code -
var activeItem = {
itemId : 0,
itemName: '',
itemAmount : 0,
itemPrice : 0
}
You see, this line creates and initialises the object activeItem. Nowhere else you have instantiated a new one. And thus every time you are modifying it, it is modifying the same item and inserting the same item. To fix, you should instantiate it inside the functions. Something like -
.....
var itemTemplate = function(){
this.itemId = 0;
this.itemName = '';
this.itemAmount = 0;
this.itemPrice = 0;
return this;
};
activeItem = null;
$(desk).on('click', '.items', function(){
activeItem = new itemTemplate();
activeItem.itemId = 0;
activeItem.itemAmount = 0;
activeItem.itemPrice = 0;
item.removeClass('selected-item');
$(this).toggleClass('selected-item');
activeItem.itemName = $(this).text();
activeItem.itemId = $(this)[0].dataset.itemid;
activeItem.itemPrice = $(this)[0].dataset.itemprice;
});
....