I have some code I got from someone for increasing and decreasing a quantity field with buttons. I'd like it to be able to make the quantity to go negative and also not have leading zeros. I'm not much on javascript. Any experts out there willing to lend a hand?
function updateQty(button) {
var parts = button.id.split(".");
var textbox = getObj("INQTY." + parts[1]);
var value = Number(textbox.value);
if (isNaN(value)) value = 0;
if (parts[0] == "increaseQty") value += 1;
if (parts[0] == "decreaseQty") value -= 1;
if (value < 0) value = 0;
value = "000" + value;
value = value.substr(value.length - 3, 3);
textbox.value = value;
textbox.modified = true;
setTimeout(function() {
textbox.focus();
}, 1);
}
if (value < 0) value = 0;
value = "000" + value;
value = value.substr(value.length - 3, 3);
The first line checks if value < 0 (after adjusting it) and if it's negative, sets it to 0. So this is what's preventing it from going negative.
The other two lines left-pad the number with zeros. They are written for a 3-digit field, so they add 3 leading zeroes to whatever the value currently is, and then use substring to take only the rightmost 3 digits. This leading zero code will not work properly if the number is negative -- but that's no problem if you're removing the leading zeroes, anyway... :-)