Skip to main content
Participant
Sep 26, 2023 1:10 PM UTC
Answered

Two dropdown menus, script to change export value based on selection?

Hello everyone,

I'm pretty new to JS/scripting in PDFs, so I would appreciate your help with this:

 

I have a PDF with two dropdown menus: "Package" and "Locations".

The package dropdown has 3 options, "S" "M" and "L" which are priced at 1999, 2499 and 2999.

The locations dropdown has 5 options, "none" and 1-4 additional locations.

 

My problem is the following: I want the export values of the additional locations to change, depending on what was selected in the package dropdown:

When "S" was chosen, 1 additional location is 499.
When "M" was chosen, 1 additional location is 699.

When "L" was chosen, 1 additional location is 899.

 

Is there a way I can achieve this? I tried to find anything and copying it into the calculation script, but I think that's totally wrong 😄 Any help would be appreciated, thank you!

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Sep 26, 2023 10:21 PM UTC

It's a little different.
The packages have fixed prices: S 1999/M 2499/L 2999
The locations should have variable values. If package S is selected, 1 location should be 499, 2 locations are 998,... If package M is selected, 1 location should be 699 and if L is selected, 1 location should be 899. 

Thanks so much for your time!


You can use something like this as the custom calculation script of your total field, then (replace all the ellipses with the actual values, of course):

 

var package = this.getField("Package").valueAsString;
var locations = this.getField("Locations").valueAsString;
if (locations=="none") event.value = "";
else {
	if (package=="S") {
		if (locations=="1") event.value = 499;
		else if (locations=="2") event.value = 998;
		else if (locations=="3") event.value = ...;
		else if (locations=="4") event.value = ...;
	} else if (package=="M") {
		if (locations=="1") event.value = 699;
		else if (locations=="2") event.value = ...;
		else if (locations=="3") event.value = ...;
		else if (locations=="4") event.value = ...;
	} else if (package=="L") {
		if (locations=="1") event.value = 899;
		else if (locations=="2") event.value = ...;
		else if (locations=="3") event.value = ...;
		else if (locations=="4") event.value = ...;
	}
}

 

6 replies

try67
Community Expert
Community Expert
Sep 26, 2023 1:26 PM UTC

Why do you want to change the export values? Is this for the purposes of a calculation that uses the value of this field, or are you export the form data to another format and want it to show the correct amount?

If the former, a better way of doing it is to use a script to perform this calculation, adding if-conditions to check the selected values in both drop-downs and perform the calculation accordingly.

Translate
Participant
Sep 26, 2023 1:31 PM UTC

Yes exactly, I want to have a sum of both values at the end, but the second value changes depending on the first value.
Right now, I have sum field that gets the export values of both dropdown fields. But it makes sense to calculate it like you said. I just don't know where to start, since I don't understand JS 😕😕

Translate
try67
Community Expert
Community Expert
Sep 26, 2023 2:49 PM UTC

So if it's only 1 additional location it's 499/699/899, and if it's more than that then it's 1999/2499/2999?

Translate