Page 1 of 1

Help with javascript

Posted: Mon Jul 15, 2013 3:46 pm
by jimr
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?

Thanks much!

Jim

Code: Select all

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);
}

Re: Help with javascript

Posted: Mon Jul 15, 2013 4:30 pm
by Scott Klement
Maybe remove these lines?

Code: Select all

  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... :-)

Re: Help with javascript

Posted: Mon Jul 15, 2013 4:55 pm
by jimr
Yeah, that worked. I could have sworn I tried that, must have done something different.

Thanks for the help. Very cool!