Skip to main content
Participant
December 8, 2017
Answered

Custom Form calculation appending last added value

  • December 8, 2017
  • 1 reply
  • 536 views

Hello everybody,

I'm getting some weird behavior when doing a custom calculation for a form field. Here is the Javascript I'm inputting:

event.value=

(this.getField("Purchase price").value-

this.getField("Loan amount").value-

this.getField("Additional loans").value-

this.getField("Deposit").value-

this.getField("Seller credits").value-

this.getField("Agent credits").value+

this.getField("Reserve req").value+

this.getField("Closing costs").value)

The problem is, the last field's value is appending to the calculated values above it. So everything calculates and display in the form field (except closing costs), and when Closing costs is filled in, the value gets appending rather than adding to the rest of the values like it should. Does anyone know how to fix this problem, or does anyone have any idea what is going on?

Thank you!

This topic has been closed for replies.
Correct answer try67

The + operator has a dual functionality in JS. When used with strings it appends them to each other (eg. "a"+"b" will return "ab"), and when used with numbers it will add them up (eg. 1 + 2 will return 3). If you use it on a string and a number it will convert the number to a string and append them, so 1 + "a" will return "1a". Even "1" + 2  will return "12", not 3.

So the solution is to explicitly convert all the values in your code to numbers, to make sure they are added up and not appended (in case any of them are strings).

To do that add a Number constructor around each value, like this:

Number(this.getField("Purchase price").value)

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 8, 2017

The + operator has a dual functionality in JS. When used with strings it appends them to each other (eg. "a"+"b" will return "ab"), and when used with numbers it will add them up (eg. 1 + 2 will return 3). If you use it on a string and a number it will convert the number to a string and append them, so 1 + "a" will return "1a". Even "1" + 2  will return "12", not 3.

So the solution is to explicitly convert all the values in your code to numbers, to make sure they are added up and not appended (in case any of them are strings).

To do that add a Number constructor around each value, like this:

Number(this.getField("Purchase price").value)

Participant
December 8, 2017

Ah ha! That was actually what I figured was happening. I believe it has to do with the fact that some of the fields were being pasted in from another form.

Let me just say thank you for your quick response, and thorough explanation. I love Adobe software, and the community behind it!