So I want something like this:
As you can see, there's a +
and -
button, to which if clicked, they show respective values appended at No. of Units
.
All in all, what I want is:
- for example, if I clicked the
+
four times, it will show12+4
(the+4
part with green color) - and if I clicked the
-
six times, it will then show12-2
(the-2
part with red color), - and if I clicked the
-
another ten times, it will stop at the value12-12
and prompt/alertDo you really want to delete this load?
(same function as thex
button at the rightmost).
The problem you see is, the adding of increment values works (if I click the +
many times), but if I decrement it, it would only stop at 0
and alternates between 0
and 1
(the operationg used is still +
and not -
though), and not to the maximum default value (which in the picture above, is 12
).
Also, if I clicked a decrement button first, it will only stop at 0
and the same case, it alternates between 0
and 1
.
Why is this? Sorry, I was not able to commit the previous two iterations of this code but both have issues too (the first one works but it appends a -n
value, thus 12--1
would appear, the second one would bypass the def
value limit, it would reach 12-14
and so on). I am at a loss here already, so I would like some help. This is my jQuery code below.
PS. I'm constructing the rows through a jQuery DataTables
, so I can't show the code that adds the rows because it is not too relevant.
PS(2). Here's the JSFiddle implementation of the above.
PS(3). It would be very appreciated if you modify the code below to your liking, as log as it FOLLOWS SUIT the THREE USE CASES STATED ABOVE. Thank you so much.
jQuery
// For increment and decrement no. of units
var def = $('#costcenter_data tr').find('td:nth-child(2)').html(); // default value or the "12" in the example
var out = def;
// Increment button
$('.btn.btn-xs.btn-primary.add-unit').click(function(e){
var count = $('#costcenter_data tr').find('td:nth-child(2)');
var value = parseInt(count.data('change_value'));
var clicked;
if (isNaN(value) && isNaN(prev)) {
value = 1;
count.data('clicked', "+")
} else {
value++;
}
var prev = count.data('clicked');
if (prev != "+") {
clicked = "-";
} else {
clicked = prev;
}
$('#costcenter_data tr').find('td:nth-child(2)').html(out+clicked+value);
count.data('change_value', value);
count.data('clicked', "+");
});
// Decrement button
$('.btn.btn-xs.btn-warning.remove-unit').click(function(e){
var count = $('#costcenter_data tr').find('td:nth-child(2)');
var value = parseInt(count.data('change_value'));
var clicked;
if (isNaN(value) && isNaN(prev)) {
value = 1;
count.data('clicked', "-")
} else if ((value-1) < 0) {
value--;
value = Math.abs(value);
if (value == parseInt(def)) {
alert("Delete prompt here");
return false;
}
} else {
value--;
clicked = count.data('clicked');
}
var prev = count.data('clicked');
if (prev != "-") {
clicked = "+";
} else {
clicked = prev;
}
$('#costcenter_data tr').find('td:nth-child(2)').html(out+clicked+value);
count.data('change_value', value);
});