Skip to main content
tad.boomer
Participating Frequently
August 9, 2020
Answered

Javascript formula Not Working

  • August 9, 2020
  • 4 replies
  • 1477 views

Total nubie here with no knowledge of javascript yet.  I have one last item I need to complete a form I am working on.   I need to take the amounts of two fields and check it against a drop down menu of Yes or No to determine if the items should be taxed 6%.   The formula someone provided to me didn't work.  It was:

var a = this.getField("Item3_Amt").value; var b = this.getField("Item4Amt").value; var c = this.getField("Dropdown").value; if(c == "No"){event.value = "$0.00"; }else event.value = (a+b)*0.6;

 

Unfortunately it doesn't work.   Can anyone tell me what the problem might be?

Also, after looking at the form a bit more I think it would work better to replace the dropdown with a check box.  So what would a working formula look like with a checkbox instead of the dropdown.

For you guys with the knowledge, your awesome!  Thanks

This topic has been closed for replies.
Correct answer Bernd Alheit

Remove the $

4 replies

tad.boomer
Participating Frequently
August 14, 2020

Hello Everyone.

I was unable to get any of the scripts to work and then the client I was making the form for decided they wanted some changes which required me to start from scratch again.  But I am back to the same problem with this formula.  This is what I put in:
var a = this.getField("Amount3").value;
var b = this.getField("Amount4").value;
var c = this.getField("Item7");
if(c.isBoxChecked(0)){event.value = (a+b)*.06;
}else event.value = "$0.00";

The formula works but when you go to put the amounts in Amount3 and Amount4 Acrobat flashes a message "The value entered does not match the format of the field (Amount7)  

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
August 14, 2020

Remove the $

tad.boomer
Participating Frequently
August 15, 2020

Thank you.  It works perfectly now.  

Nesa Nurani
Community Expert
Community Expert
August 9, 2020

I'm assuming that you want if checkbox is checked tax is calculated?

use this code:

var a = this.getField("Amount3").value;
var b = this.getField("Amount4").value;
var c = this.getField("Enter Checkbox Field Name Here");
if(c.isBoxChecked(0)){event.value = (a+b)*.06;
}else event.value = "$0.00";

 

Also consider formating your field to number and add currency simbol ( as ls_rbls mention in this post and try67 in your first post) in that case replace "$0.00" with "0"

Nesa Nurani
Community Expert
Community Expert
August 9, 2020

You didn't have to open new post, you could just ask in your first thread what the problem was with the code and I would gladly help you, also in my code there is *.06 and not *0.6

tad.boomer
Participating Frequently
August 9, 2020

I apologize for the duplication.  
Could you tell me what this formula would look like substituting a check box for the drop down?

ls_rbls
Community Expert
Community Expert
August 9, 2020

That is a  different question tad_boomer.

 

As mentioned by Nesa you should ask in the original thread.

 

Also consider opening a new thread since now you want to throw a checkbox in the mix.

 

Are you looking into learning JavaScript or are you asking for someone to write the code for you?

 

ls_rbls
Community Expert
Community Expert
August 9, 2020

+++EDITED REPLY  , FIXED AN ERROR IN THE LAST LINE OF THE SCRIPT

 

Just place the script in the custom calculation script section of the field that will be performing the check. 

 

Like this:

 

 

var a = this.getField("Item3_Amt").value; 
var b = this.getField("Item4_Amt").value; 
var c = this.getField("Dropdown36").valueAsString;

 if(c == "yes") event.value = (a+b)*0.06;
else event.value = 0;


 

 

 

Note that 6%  is not calculated with 0.6.

 

Also, all you need to do to have the empty calculated field display "$0.00" is use the built-in fomat scheam for your numbers.

 

See slide below: