The point is i have radio button that send value to an array with push method, when it is clicked.
The problem is the radio button became like an increment and decrement to the value. How do i fix this ? Sorry for my bad english
I have tried use 'pop' method to take back the value.
var total = document.getElementById("total");
var subTotal = [];
var toppings = {
'Avocado': [true, false, false],
'Tuna': [true, false, true],
'Duck': [false, false, true],
'Sausage': [false, false, true],
'Lobster': [false, true, false],
'Oyster': [false, true, false],
'Salmon': [false, true, false],
'Bacon': [false, true, true]
};
function pizzaChoice(menuId) {
var menu = document.querySelectorAll('input[name="pizza"]');
if (menu[menuId].checked) {
subTotal.push(parseInt(menu[menuId].value));
} else {
subTotal.pop();
};
for (const name of Object.keys(toppings)) {
var chkbox = document.getElementById(name);
if (toppings[name][menuId]) { // available topping on this menu
chkbox.disabled = false;
}
else {
chkbox.disabled = menu[menuId].checked;
if (menu[menuId].checked) {
chkbox.checked = false;
}
}
}
}
function pickSize() {
var foo = document.getElementsByName("size");
for (let i = 0; i < foo.length; i++) {
if (foo[i].checked) {
subTotal.push(parseInt(foo[i].value));
} else {
subTotal.pop();
}
}
}
function pickTopping(toppingName) {
var x = document.getElementById(toppingName);
if (x.checked) {
subTotal.push(parseInt(x.value));
} else {
subTotal.pop();
}
}
function calc() {
if (!subTotal.length == 0) {
var grandTotal = subTotal.reduce(function (a, b) {
return a + b;
}, 0);
total.innerHTML = grandTotal;
};
};