Custom Java Script Calculation for Radio Buttons
Copy link to clipboard
Copied
Hi all, I'm extremely new to this and it's generally outside of my wheelhouse so I'm looking for a bit of help. I'm hoping to create a budget form that allows people to select whether or not the item will be charged Harmonized Sales Tax (which is 13% where I'm from). Not all items are taxed so I'm allowing them to choose via a radio buttom whether tax is applicable. I'd like if they choose 'Y' the subtotal should calculate the 13% tax, and if they choose 'N' I'd want it to simply calculate based on the Cost Per and Quantity of the item.
I'm able to calculate the Quantity and Cost per into a subtotal very easily obviously, but I'm somewhat lost as to how I can use the radio buttons to calculate the percentage tax. Any help is much appreciated.
Copy link to clipboard
Copied
Hi,
This is an old thread that seems like it fell through the cracks. I hope by now you were able to figure it out.
If not, here are a few ideas:
- Make your radio buttons mututally exclusive: https://www.youtube.com/watch?v=nHT2H9_G3pg
- You make radio buttons mutually exclusive by labeling them with the same field name (both YES & NO radio buttons will have the same name. I will use for the name "radioButton"). Then you add a customed export value.
- To add a custom export value right-click on the radio-button object and then select "Properties"from the context menu. Then go to the "Options" tab. In the blank labeled "radioButton" type the capital "Y" for example. Then repeat this step in the other radio button and type in "N". See the slide below:
- After this is done, all you have to do now is use a custom calculation script for the SUBTOTAL field next to the radio buttons. Right-click on the subtotal text field, select "Properties" from the context menu, and click on the "Calculate" tab. Down below in that dialogue box, tick the radio button "Custom calculation script:" and then on the "Edit..." button next to it.
- This will open up the javascript editor for that field. You can copy the following script (or similar) below and paste it in the JavaScript editing window:
var a = this.getField("radioButton").value;
var c = this.getField("Cost per Item").value;
var d = this.getField("Cost per Quantity").value;
if (a != "Y") event.value = c+d;
if (a != "N") event.value = (Math.ceil(c+d)*(13/100))+(c+d);
if ( (c =="") || (d =="") ) event.value ="";
Copy link to clipboard
Copied
I would reconsider the way this code is built. For starters, why check a negative condition when you can use the positive? It's just more confusing. Also, I would use an if-else if-else construction, instead of separate if-commands. That way there would be a default value used for the field, in case none of the conditions is met.
Copy link to clipboard
Copied
I tried the IF , ELSE conditions but with my current knowledge it was throwing back undesirable results. I 'm sure I wasn't following appropriate guidance.
Would you mind sharing an example?
Copy link to clipboard
Copied
I feel really dumb. Sometimes I really struggle understanding what people mean when they write.
So here it is:
var a = this.getField("radioButton").value;
var c = this.getField("Cost per Item").value;
var d = this.getField("Cost per Quantity").value;
if (a === "N") event.value = c+d;
else if (a === "Y") event.value = (Math.ceil(c+d)*(13/100))+(c+d);
else if ( (c =="") || (d =="") ) event.value ="";
This also works:
var a = this.getField("radioButton").value;
var c = this.getField("Cost per Item").value;
var d = this.getField("Cost per Quantity").value;
if (a === "N") {event.value = c+d;
} else {
if (a === "Y") { event.value = (Math.ceil(c+d)*(13/100))+(c+d);
} else {
if ( (c =="") || (d =="") ) event.value ="";
}
}
Thank you for continued mentoring. You're a beast!
Copy link to clipboard
Copied
Don't sweat it. It's a learning curve and you're doing pretty well.
I prefer the first code you posted. However, I would suggest you think of the following scenario: What will happen if none of those conditions is met? ie, a is neither "N" or "Y", and both c and d are not empty. What value will be assigned to the field then?
Copy link to clipboard
Copied
Ok this is what is working on my end:
var a = this.getField("radioButton").value;
var c = this.getField("Cost per Item").value;
var d = this.getField("Cost per Quantity").value;
if ( (a === "N") && (c && d !="") )event.value = c+d;
else if( (a === "Y") && (c && d !="") ) event.value = (Math.ceil(c+d)*(13/100))+(c+d);
else if ( ( a === "Y") || (a === "N") && (c || d =="") ) event.value ="";
else if ( ( a === "Y") || (a === "N") && (c && d =="") )event.value ="";
I'm gonna try JR's method.
Copy link to clipboard
Copied
Change the last line to just:
else event.value ="";
That covers all other situations.
Copy link to clipboard
Copied
great! thanks.
Copy link to clipboard
Copied
Use "1" as the export value for the "No" button, and use "1.13" as the export value for the "Yes" button.
So you just have to multiply the field "Quantity" by thefield "Cost per" by the button value.
This does not requires JavaScript.
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
This is with JR's suggestion:
var a = this.getField("radioButton").value;
var c = this.getField("Cost per Item").value;
var d = this.getField("Cost per Quantity").value;
if ( (a == "1") && (c && d !="") )event.value = c+d;
else if( (a == ".13") && (c && d !="") ) event.value = (Math.ceil(c+d)*(a))+(c+d);
else if ( ( a === ".13") || (a === "1") && (c || d =="") ) event.value ="";
else event.value ="";
Copy link to clipboard
Copied
"Zero", "Off" and "empty" values are automatically managed by Acrobat's buit in calculations
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
Thank you!
Copy link to clipboard
Copied
Tip: a set of radiobuttons cannot return "Off" if you tick one as default.
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
Awesome! thanks for that tip.

