I'm having an issue regarding my total div using JQuery.
Actually this div changes values when + or - buttons are clicked, which actually works well.
My problem here is that when i "reset" a value to 0 after i augmented it, as soon as this value gets to 0, my total gets equal to NaN, where i want it to stay at 0 to keep calcullating total for my other values.
window.onload = function() {
var ids = new Array();
$('input').each(function() {
ids.push($(this).attr("id"));
});
var total = 0;
$('.add').click(function() {
var quant, idprev, price, tot;
if ($(this).prev().val() < 99) {
$(this).prev().val(+$(this).prev().val() + 1);
quant = ($(this).prev().val());
idprev = ($(this).prev().attr('id') - 1);
price = $("#" + idprev).val();
tot = quant * price;
$("#result" + idprev).html(tot.toFixed(2) + "€");
for (var i = 0; i < ids.length; i++) {
if (ids[i] % 2) {
total = total + (tot / quant);
$("#total").html("Total : " + total.toFixed(2) + "€");
} else {
return false;
}
}
}
});
$('.sub').click(function() {
var quant, idprev, price, tot;
if ($(this).next().val() > 0) {
if ($(this).next().val() > 0)
$(this).next().val(+$(this).next().val() - 1);
quant = ($(this).next().val());
idprev = idprev = ($(this).next().attr('id') - 1);
price = $("#" + idprev).val();
tot = quant * price;
$("#result" + idprev).html(tot.toFixed(2) + "€");
for (var i = 0; i < ids.length; i++) {
if (ids[i] % 2) {
total = total - (tot / quant);
$("#total").html("Total : " + total.toFixed(2) + "€");
} else {
return false;
}
}
}
});
}
<script data-require="jquery@3.1.1" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" />
<div id="total"></div>
<div class="main">
<table>
<tr>
<td align="center">Description</td>
<td align="center">Price</td>
<td align="center">Quantity</td>
</tr>
<tr>
<td>Article 1</td>
<td><input type="text" hidden="true" value="2.5" id="1">2.50€</td>
<td>
<div class="quantity buttons_added">
<button type="button" id="sub" class="sub">-</button>
<input type="number" id="2" value="0" min="0" max="3" />
<button type="button" id="add" class="add">+</button>
</div>
</td>
<td>
<div id="result1"></div>
</td>
</tr>
<tr>
<td>Article 2</td>
<td><input type="text" hidden="true" value="1" id="3">1.00€</td>
<td>
<div class="quantity buttons_added">
<button type="button" id="sub" class="sub">-</button>
<input type="number" id="4" value="0" min="0" max="3" />
<button type="button" id="add" class="add">+</button>
</div>
</td>
<td>
<div id="result3"></div>
</td>
</tr>
<tr>
<td>Article 3</td>
<td><input type="text" hidden="true" value="3.8" id="5">3.80€</td>
<td>
<div class="quantity buttons_added">
<button type="button" id="sub" class="sub">-</button>
<input type="number" id="6" value="0" min="0" max="3" />
<button type="button" id="add" class="add">+</button>
</div>
</td>
<td>
<div id="result5"></div>
</td>
</tr>
<tr>
<td>Article 4</td>
<td><input type="text" hidden="true" value="1" id="7">1.00€</td>
<td>
<div class="quantity buttons_added">
<button type="button" id="sub" class="sub">-</button>
<input type="number" id="8" value="0" min="0" max="3" />
<button type="button" id="add" class="add">+</button>
</div>
</td>
<td>
<div id="result7"></div>
</td>
</tr>
</table>
</div>
Thanks in advance :)