A
A
Artem2020-04-28 07:15:01
JavaScript
Artem, 2020-04-28 07:15:01

How to change the value of undefined = 0 or = 1?

When pressing the button with attr "DATA-ART" the value is added. When everything is standard, then a variable and an empty object "var cart = {};" empty, although set ( else{cart[articul] = 1; } ) and each attr taken from the "DATA-ART" array "data" is undefined . Most likely I'm missing something. I would be grateful for an explanation.

The main array "data" which refers to the json file where all the keys and values ​​.

button class="number" data-num="' + key + '">' + cart[key] + 'button;
button class="plus" data-art="' + key + '" +button;

function plus() {
var articul = $(this).attr('data-art');
if (cart[article]!
=undefined) { cart[article]++;
}
else{
cart[article] = 1;
}
localStorage.setItem('cart', JSON.stringify(cart));
load Goods();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
victorpzdnf, 2020-04-28
@shylov

undefined means the value was not found. It's not a variable.

button class="number" data-num="' + key + '">' + cart[key] + 'button;
button class="plus" data-art="' + key + '" +button;

function plus() {
var articul = $(this).attr('data-art');
if (isNaN(cart[articul])) {
cart[articul]++;
}
else {
cart[articul] = 1;
}
localStorage.setItem('cart', JSON.stringify(cart));
loadGoods();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question