Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

Javascript - Dropdown menu resetting each time a new item is selected.

Community Beginner ,
Nov 15, 2021 Nov 15, 2021

I have a form that is requiring the user to fill in the Project Name field (Text56) ..
Example: Project 1, Project 2,  Project 3

 

The Dropdown menus are then using the following code to set the menu items as the above project names..

 

event.target.setItems( this.getField("Text56").value.split(", "));

 

I need the user to be able to select one of these menu items per drop down field...
Example:

Monday_Dropdown1.0 = Project 1
Tuesday_Dropdown1.0 = Project 3

However, when Monday_Dropdown1.0 is set to Project 1 for example.... As soon as another dropdown box is selected.... the Monday field is reset to blank.

Is this something wrong with my code?
Could someone help urgently? I would really appreciate it.
Screenshot 2021-11-15 at 10.18.13.pngScreenshot 2021-11-15 at 10.24.52.png

TOPICS
Cancel subscription , Crash or freeze , Create PDFs , Edit and convert PDFs , General troubleshooting , How to , Install update and subscribe to Acrobat , JavaScript , PDF forms , Print and prepress , Rich media and 3D , Scan documents and OCR , Security digital signatures and esignatures , Standards and accessibility
2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Nov 15, 2021 Nov 15, 2021

You have to first remove all the calculation scripts you already added. The only script you should be using is under the text field's Validation event.

To apply the same values to multiple fields you can use something like this:


for (var i=0; i<=19; i++) {
	if (event.value) {
		this.getField("Monday_Dropdown1."+i).setItems(event.value.split(", "));
		this.getField("Tuesday_Dropdown1."+i).setItems(event.value.split(", "));
		// etc.
	} else {
		this.getField("Monday_Dropdown1."+i).clearItems();
		this.getField("Tuesday_Dropdown1."+i).clearItems();		
		// etc.
	}
}

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2021 Nov 15, 2021

event.target.setItems( this.getField("Text56").value.split(", "));

 

Where does you this script?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 15, 2021 Nov 15, 2021

It is entered into each dropdown field under 'Calculate' tab > Custom Calculation Script

Screenshot 2021-11-15 at 10.35.49.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2021 Nov 15, 2021

A calculation script is executed each time the value of any field in the file is changed. That's why the value keeps resetting. You should move the code to the custom Validation script of the text field and adjust it accordingly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 15, 2021 Nov 15, 2021

Ahh, was awaiting a response from you! ha.

 

So is my code ok to just move to the Text56 field? or do I need to add additional information

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2021 Nov 15, 2021

You need to adjust it slightly. Try this:

 

if (event.value) this.getField("Tuesday_Dropdown1.0").setItems(event.value.split(", "));

else this.getField("Tuesday_Dropdown1.0").clearItems();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 15, 2021 Nov 15, 2021

I tried that into the Text56 field and also the Tuesday Dropdown validation area separately but it doesn't seem to work. my menu options no longer pull through.

 

am I doing something wrong?

 

Screenshot 2021-11-15 at 11.10.38.png

 

 

will I need to adjust it for each field name and enter into the validation area for each of these?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2021 Nov 15, 2021

It only needs to be under the text field. Remove the Validation (and Calculation!) scripts from the drop-down.

If it still doesn't work share the file, please.

 

If you want each text field to populate a different drop-down then you'll need to duplicate the code, yes, or create a generic function (maybe taking the name of the target drop-down field as a parameter) and then call it from each text field.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 15, 2021 Nov 15, 2021

So there will only be 1 text field in which user will enter multiple project names separated by space & a comma ( ,)


I need each of the dropdown fields to have the menu populated from this 1 text field and yes need each dropdown to have a different selection.

I have shared the or file for you to take a look 

 

So I guess I would need to have a code that lists all the dropdown fields rather than just Tuesday

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2021 Nov 15, 2021

You have to first remove all the calculation scripts you already added. The only script you should be using is under the text field's Validation event.

To apply the same values to multiple fields you can use something like this:


for (var i=0; i<=19; i++) {
	if (event.value) {
		this.getField("Monday_Dropdown1."+i).setItems(event.value.split(", "));
		this.getField("Tuesday_Dropdown1."+i).setItems(event.value.split(", "));
		// etc.
	} else {
		this.getField("Monday_Dropdown1."+i).clearItems();
		this.getField("Tuesday_Dropdown1."+i).clearItems();		
		// etc.
	}
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 15, 2021 Nov 15, 2021
LATEST

I cannot thank you enough....

That all worked perfectly!

 

Have a lovely day @try67 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines