These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Simple subtraction Javascript needed

StJay
Registered: Aug 15 2008
Posts: 3

I have just about pulled out all my hair at this point. I have tried and tried, but this just seems to be beyond me. I have read many examples of Javascripting so far today and I just don't seem to be able to do this one simple thing. I use the built-in sum/multiply functions in Acrobat 8 Pro, which work well and I tried the Simplified Field Notation for subtracting, but it won't run, so I thought I would wade into to Javascript. Well, maybe not.

I have copied in several examples below - my fields are named "total claims needing to be paid" and "total plan payments" which I substituted instead of "Text1" and "Text2".

if (event) { // get values from two text fields
var a = Number(this.getField('Text1').value);
var b = Number(this.getField('Text2').value);
// subtract the values and show it
this.event.target.value = a - b;

and

var a=this.getField ("total claims needing to be paid");
var b=this.getField ("total plan payments");
even.value=a.value-b.value;

Neither of these works for my situation and I don't yet know enough to build my own script, but I'm working on it. The problem is I need this to work as soon as possible. I just finished a big, long acrobat form that has been a long time in the making. This is the last thing I need to do.

I tried the Field Notation that Dimitri provided of Text1-Text2, which didn't work either.

Could this be a problem because the "a" and the "b" fields are both calculated fields (+) themselves. I want to subtract "b" from "a". Could it be a field calculation ordering problem?

Any advice would be appreciated. thanks.

Diane

My Product Information:
Acrobat Pro 8.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
The second one should work, assuming that "even.value" is a typo and you really used "event.value".

It could very well be the field calculation order is incorrect. Have you confirmed it's OK?

George
StJay
Registered: Aug 15 2008
Posts: 3
Yes George, it was a typo in my post, not in the script itself as entered in Acrobat. The problem was the caluculation ordered. It works just fine now. Thanks for your input!

Diane
Josman
Online
Registered: Jul 15 2009
Posts: 31
Hi Gorge I have a problem whit this javaScript, I need to substract two fiels one call Family and the other one call Boy and Unit and I wrote this javascript but it is not working, please I need your help.
Thanks
if (event) { // get values from two text fields
var a = Number(this.getField('Family').value);
var b = Number(this.getField('Boyandunit').value);
// subtract the values and show it
this.event.target.value = a - b;


}

Josman

try67
Expert
Registered: Oct 30 2008
Posts: 2398
What's the point of if(event) ? Drop that part, and also change the last line from this.event.target.value to event.target.value

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Josman wrote:
Hi Gorge I have a problem whit this javaScript, I need to substract two fiels one call Family and the other one call Boy and Unit and I wrote this javascript but it is not working, please I need your help.
Thanks
if (event) { // get values from two text fields
var a = Number(this.getField('Family').value);
var b = Number(this.getField('Boyandunit').value);
// subtract the values and show it
this.event.target.value = a - b;


}
'event' is an object in Acrobat JavaScript, you need to add a property or method to it so JavaScript knows what to provide. You will also want the '(statement)' to resolve to either a true of false state, since this is the required result for the 'statement' parameter in the 'if' statement.

Are you getting any errors or messages on the JavaScript console?

I receive a message indicating that there is a missing '}' on line 9

George Kaiser

Josman
Online
Registered: Jul 15 2009
Posts: 31
HI Gorge
I retype what you sid and it work
This is what I wrote
//get the numeric value of the following fields
var A = Number(this.getField("A").value);
var B = Number(this.getField("B").value);
var C = Number(this.getField("C").value);
event.value = A-B-C;
Thank you.
I have another Question.
I want to populate a single text field from a combo box.
A combo box lis has values it is like this.
Trek = 300, Boy Scout 270 etc.
I want that only the number will show in the text field. How do I set up this?
Thanks for your help one more time.

Josman

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
You can set the item text and the optional export value in a combo box. Then after you select the item text, the value of the combo box field is the export value for the item selected. If there is no export value provided for the item text entered the entered value becomes the export value.

George Kaiser