Skip to main content
Participant
November 25, 2023
Answered

Dynamic Dropdown Items

  • November 25, 2023
  • 5 replies
  • 2054 views

Hello,

 

I like to create a set of Dropdown fields having the same item lists. But once I select "Item A" in the first Dropdown the rest of them will show all items except "Item A" in their list. Likewise if I select "Item B" is the second Dropdown, the rest should show all items except "Item A" and "ItemB". This way the loop should go on to the last Dropdown which would have only one item (left to select) to show.

 

This process should work as per order of the Dropdown Fields. Each Dropdown must have a item of blank selection (Null Value). The next Dropdown would only unlock if a selection in made to the previous Dropdown.

 

Please help me with the javascript coding...

 

Thanks in advance.

Correct answer bebarth

Hi,

Here is an example for a set of drowpdown fields with a same name including an index. In the example file, this script is included in a function which is recalled by a "Mouse Enter" Action script: fillDropdown();

 

function fillDropdown() {
	var theItems=["Item A","Item B","Item C","Item D","Item E","Item F","Item G","Item H","Item I","Item J"];
	var dropdownName="Dropdown";
	var	dropdownNumber=5;
	var theIndex=event.target.name.substr(event.target.name.indexOf(".")+1);
	var theValue=event.target.value;
	for (var i=0; i<dropdownNumber; i++) {
		if (i!=theIndex) {
			for (j=0; j<=theItems.length; j++) {
				if (theItems[j]==this.getField(dropdownName+"."+i).value) {
					theItems.splice(j,1);
					break;
				}
			}
		}
	}
	theItems.unshift("- Select an Item -");
	event.target.setItems(theItems);
	event.target.value=theValue;
}

 

 If the name of each field is not consistent, the script can be be modifyed and names placed in an other array!

@+

5 replies

bebarth
Community Expert
bebarthCommunity ExpertCorrect answer
Community Expert
November 25, 2023

Hi,

Here is an example for a set of drowpdown fields with a same name including an index. In the example file, this script is included in a function which is recalled by a "Mouse Enter" Action script: fillDropdown();

 

function fillDropdown() {
	var theItems=["Item A","Item B","Item C","Item D","Item E","Item F","Item G","Item H","Item I","Item J"];
	var dropdownName="Dropdown";
	var	dropdownNumber=5;
	var theIndex=event.target.name.substr(event.target.name.indexOf(".")+1);
	var theValue=event.target.value;
	for (var i=0; i<dropdownNumber; i++) {
		if (i!=theIndex) {
			for (j=0; j<=theItems.length; j++) {
				if (theItems[j]==this.getField(dropdownName+"."+i).value) {
					theItems.splice(j,1);
					break;
				}
			}
		}
	}
	theItems.unshift("- Select an Item -");
	event.target.setItems(theItems);
	event.target.value=theValue;
}

 

 If the name of each field is not consistent, the script can be be modifyed and names placed in an other array!

@+

Participant
November 27, 2023

Thanks Dear,

Actually I have littile coding knowledge. Your file works fine, just update of list items in the succeeding dropdowns does not happen instantly. Rather the list only updates when I make the previous dropdown selection again. 

 

However, I made this sort of dependent dropdowns using visual basic earlier. Javascript syntax in adobe coding is not familiar to me. Can you please give me any reference manual for adobe pdf javascript.

 

The way I did it:

I put the list itmes in an Array Variable.

For the dropdown names I used the Array Index as suffix of the names

(e.g. drop0,drop1,drop2 etc. for i = 0 to 2, drop & i ) concatenating dropdown name and variable value.

Running the 'for next' loop I add list items while checking with a nested 'if...else....next i' clause to skip the previously selected item.

'else if' can be used for multiple match to make.

 

But with javascript and adobe environment I am at total lost with the coding syntax !!!

 

Thanks once again for your kind response. Please share any javascript manual for adobe pdf environment if available.

 

Take Care.

 

 

bebarth
Community Expert
Community Expert
November 27, 2023

Hi,

quote
... Your file works fine, just update of list items in the succeeding dropdowns does not happen instantly.
By @Khalid Khan

Sorry, you must tick "Commit selected value immediately" as option for all the dropdown fields. I forgot to do it in my prvious example file. Attached is a new one which should work better...

@+

Thom Parker
Community Expert
Community Expert
November 25, 2023

Here'a a "Not Free" example file that shows exactly how to do this:

https://www.pdfscripting.com/public/Multiple-Lists-Unique-selection-Description.cfm

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
November 25, 2023

[can be removed]

try67
Community Expert
Community Expert
November 25, 2023

This is going to be tricky to implement, especially if the user goes back and changes their initial selection(s), in which case you'll need to re-apply all the lists, re-select the values (if possible!), etc.

A better (and much easier to implement) approach is to prevent the user from making a selection if that item was already selected before. That way you don't have to set and reset the lists each time they change their selection.

Participant
November 27, 2023

Thanks Dear,

You are absolutely right!

At first I tried to find a way of grouping dropdowns, so that the selections can be made to unique items only (as like the option buttons work).

I didn't find any such option. Please let me know if any workaround is available of javascript coding which is definitely much easier to implement.

 

Take Care.

 

Nesa Nurani
Community Expert
Community Expert
November 25, 2023

How many dropdown fields you have, and what are their names?

 

Participant
November 27, 2023

Salam,

I only need four such dropdown fields and names can be like: drop1, drop2, drop3 and drop4

 

Take Care.