Skip to main content
Participating Frequently
February 29, 2024
Answered

Dropdowns dependent on each other?

  • February 29, 2024
  • 2 replies
  • 5438 views

Is it possible to create multiple dropdowns that are dependent on each other in terms of their values? 

 

I have dropdowns that are used to create an ordered list where I want to ensure that if a value is used in one of the dropdowns it can't be used in another dropdown. 

 

For example:

dropdown1 has values of 1,2,3,4,5, etc... and if say #1 was selected then dropdown2 would have all of the other values EXCEPT #1 and then so on and so forth for as multiple dropdowns until the end user gets to the final dropdown option and they only have the last value available.

 

I'm assuming this is some kind of javascript but I have no idea what it would look like and have 0 experience actually writing a javascript. 

This topic has been closed for replies.
Correct answer try67

I have 1 additional question: 

 

How could this be used as a document level script so that it doesn't have to be added to each dropdown. Just wondering if there is a way to not have to do this for 20+ dropdowns. 

 

If that is not a thing then not a problem, would still rather have this then have incorrect data on the forms we are using. 


To do that you can add this function as a doc-level script:

 

function validateDropdownsGroup() {
	var allFields = ["Dropdown1", "Dropdown2", "Dropdown3"]; // etc.
	if (event.value!=event.target.defaultValue) {
		for (var i in allFields) {
			if (allFields[i]==event.target.name) continue;
			if (this.getField(allFields[i]).valueAsString==event.value) {
				app.alert("This value was already selected in one of the other fields. Choose another, please.");
				event.rc = false;
				break;
			}
		}
	}
}

 

And then call it from the Validation event of each one of the fields in the list. If you don't to apply this call manually, you can use a script to do it, too, like this (run this code from the JS Console, for example):

 

var allFields = ["Dropdown1", "Dropdown2", "Dropdown3"]; // etc.
for (var i in allFields) {
	this.getField(allFields[i]).setAction("Validate", "validateDropdownsGroup();");
}

2 replies

try67
Community Expert
Community Expert
February 29, 2024

It's tricky to actually filter the lists. Much easier to use a Validation script to reject a value if it was already selected in one of the other fields.

Participating Frequently
March 1, 2024

How would I go about doing that / what would that kind of script be? 

Would it be a document level script or something that would be in each dropdown? 

try67
Community Expert
Community Expert
March 1, 2024

It would be under each field (although it is possible to use a doc-level script for it).

Here's a simple example with 3 fields named "Dropdown1" to "Dropdown3" and the same values (I assume there's a default "blank" value in each one, though).

As the custom Validation event of the first one you can use the following code:

 

var otherFields = ["Dropdown2", "Dropdown3"];
if (event.value!=event.target.defaultValue) {
	for (var i in otherFields) {
		if (this.getField(otherFields[i]).valueAsString==event.value) {
			app.alert("This value was already selected in one of the other fields. Choose another, please.");
			event.rc = false;
			break;
		}
	}
}

 

You just need to adjust the names of the other fields in the first line of the code to use it for the other fields.

Abambo
Community Expert
Community Expert
February 29, 2024

It does not look utterly complicated, if you go sequentially (i.e. the first dropdown box first). If you really need a random selection, it will be OK with three lists, but it gets rather complex with more. Furthermore, the last one does not need to be a dropdown, except if there is still a choice left.

ABAMBO | Hard- and Software Engineer | Photographer