Skip to main content
Participant
December 19, 2022
Answered

Javascript, checkbox for senior discount on sum

  • December 19, 2022
  • 3 replies
  • 931 views

Hello,

 

Im making a fillable form. The user is able to sum theirs costs by entering numbers in text boxes and the total value gets into "Textbox 4".

 

If they are a senior, I want the user to be able to use "Check Box1", and this sum will be multiplied by 0.5 in another box "Final price". If not, I want the sum to be multiplied by 0.1 and get into the box "Final price".

 

Or is there a better way to do simple senior discounts?

 

Much appricieated.

 

 

This topic has been closed for replies.
Correct answer Thom Parker

First I'd suggest you give the form fields meaningful names.  It'll make scripting and form maintenance easier. 

 

To solve your issue, enter this script into the custom calculation for the "Final Price" field.

 

event.value = this.getField("Textbox 4").value *( (this.getField("Check Box1").value == "Off")?0.1:0.5);

The field names used in the script must exactly match the field names on the form. 

 

 

3 replies

Participant
December 20, 2022

Is there a better way to do this without the checkbox?

Simply put. I just want the user to be able to see what discount they are getting on their aggregated sum.

Nesa Nurani
Community Expert
Community Expert
December 20, 2022

Is there other option for users to choose they are seniors other then checkbox?

You can create "Discount" field between "Textbox 4" and "Final Price" and use this script in "Final Price" it will show 10% or 50% if checkbox is checked:

this.getField("Discount").value = this.getField("Check Box1").valueAsString == "Off" ? "10%" : "50%";
event.value = this.getField("Textbox 4").value *( (this.getField("Check Box1").value == "Off")?0.9:0.5);

Nesa Nurani
Community Expert
Community Expert
December 20, 2022

If checkbox is not checked, you wish to sum remain as in "Textbox 4" ? Because multiply by 0.1 is 90% discount.

Participant
December 20, 2022

Oh yeah... I wanted to give some discount for non seniors as well. So, then I should do 0.9:05?


If I want 10% of the costs for regulars. 50% for seniors?

 

Thank you for great help

 

Participant
December 20, 2022

Disregard this.... im bad at math.

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
December 19, 2022

First I'd suggest you give the form fields meaningful names.  It'll make scripting and form maintenance easier. 

 

To solve your issue, enter this script into the custom calculation for the "Final Price" field.

 

event.value = this.getField("Textbox 4").value *( (this.getField("Check Box1").value == "Off")?0.1:0.5);

The field names used in the script must exactly match the field names on the form. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
December 20, 2022

If the box is unchecked. It does not do the 10% calculation. For the 10% calculation to be made, I have to check and uncheck the box.