In the first click, the code not finding the "item.id". "items" is come from LocalStorage, and it is not NULL
console.log(items);
$('.user-basket__product-delete>span.icon-trash').click(function(){
let n = $(this).parents('.user-basket__product').attr('data-id');
console.log(items);
$.each(items, function(index,item){
if(item.id === parseInt(n)){
items.splice(index,1);
localStorage.setItem('items',JSON.stringify(items));
$.priceEvents.totalPriceEvent(items);
}
})
$(this).parents('.user-basket__product').remove();
setTimeout(function() {
$.noContent();
}, 200);
})
and the bug
Uncaught TypeError: Cannot read property 'id' of null
at all.js:4075
at Function.each (all.js:169)
at HTMLSpanElement.<anonymous> (all.js:4074)
at HTMLSpanElement.dispatch (all.js:1945)
at HTMLSpanElement.h.handle (all.js:1883)
(anonymous) @ all.js:4075
each @ all.js:169
(anonymous) @ all.js:4074
dispatch @ all.js:1945
h.handle @ all.js:1883
and the items in the console before click and after the first click. I realized now, the code deleted the item.id in LocalStorage in the first click but return 3-element array. After a second click, remove parents element and fix array
(3) [{…}, {…}, {…}]
0: {id: 5, name: "Product 4", detail: "Lorem ipsum dolor sit amet", price: "99", cargo: "9.95", …}
1: {id: 3, name: "Product 2 Lorem Ipsum mix Pro", detail: "Lorem ipsum dolor sit amet", oldPrice: "110", price: "99", …}
2: {id: 4, name: "Product 3", detail: "Lorem ipsum dolor sit amet", price: "99", cargo: "9.95", …}
length: 3
__proto__: Array(0)
all.js:4074
(3) [{…}, {…}, {…}]
0: {id: 3, name: "Product 2 Lorem Ipsum mix Pro", detail: "Lorem ipsum dolor sit amet", oldPrice: "110", price: "99", …}
1: {id: 4, name: "Product 3", detail: "Lorem ipsum dolor sit amet", price: "99", cargo: "9.95", …}
length: 2
__proto__: Array(0)