Help with javascript

Use this board to ask questions or have discussions with other Rich Displays users.
Post Reply
jimr
New User
Posts: 13
Joined: Sun Feb 05, 2012 5:44 pm
First Name: Jim
Last Name: Rohde
Company Name: TMW Systems
Phone: 440-721-2931
Address 1: 6350 Quadrangle Dr
Address 2: Suite 300
City: Chapel Hill
State / Province: North Carolina
Zip / Postal Code: 27517
Country: United States
Contact:

Help with javascript

Post 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);
}
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Help with javascript

Post 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... :-)
jimr
New User
Posts: 13
Joined: Sun Feb 05, 2012 5:44 pm
First Name: Jim
Last Name: Rohde
Company Name: TMW Systems
Phone: 440-721-2931
Address 1: 6350 Quadrangle Dr
Address 2: Suite 300
City: Chapel Hill
State / Province: North Carolina
Zip / Postal Code: 27517
Country: United States
Contact:

Re: Help with javascript

Post by jimr »

Yeah, that worked. I could have sworn I tried that, must have done something different.

Thanks for the help. Very cool!
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests