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

Button for Including/Deleting an option in a dropdown list in a Form

Explorer ,
Oct 24, 2023 Oct 24, 2023

Hello, is it possible to have a button in a form that when clicked, you can add/delete an option in your dropdown list? (instead of going to options of a dropdown list). can you give me example of a script if possible. Thanks!

Expected Output:

 

Type input in text field > Pressing the Add button Will Add the input in dropdown list

TOPICS
Create PDFs , Edit and convert PDFs , JavaScript , Modern Acrobat , PDF , PDF forms
2.5K
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Oct 24, 2023 Oct 24, 2023

Use like this:

 

var drop = this.getField("Dropdown5");
var str = this.getField("text4").valueAsString;
drop.insertItemAt(str,"", 0);

 

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 ,
Oct 25, 2023 Oct 25, 2023

Ok, first there was an error in my script above, it's fixed now so use new script.

To sort items, you can use the correct answer from this post:

https://answers.acrobatusers.com/Alphabetic-order-drop-list-q255525.aspx 

When you add document level script, add this line to the script I posted above:

ListEntrySort(drop.name);

Script in button should look like this:

var drop = this.getField("Dropdown5");
var str = this.getField("text4").valueAsString;
drop.insertItemAt(str,"", 0);

ListEntrySort(drop.name);

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 ,
Oct 24, 2023 Oct 24, 2023

You can use insertItemAt() to add item to dropdown and deleteItemAt() to delete item from dropdown.

0 = first item in the list.

-1 = last item in the list.

Example of adding item to top of the list:

var drop = this.getField("Dropdown");
drop.insertItemAt("Orange", 0);

Same for deleting item, if no number is set it will delete the currently selected item.

Example of deleting currently selected item, then select the top item in the list:

var drop = this.getField("Dropdown");
drop.deleteItemAt();
drop.currentValueIndices = 0;

 

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
Explorer ,
Oct 24, 2023 Oct 24, 2023

thank you,

can you see if this is right (this is the output I want) I tried the script but I always got error
btw this is my expected output

avv98_0-1698213788413.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 ,
Oct 24, 2023 Oct 24, 2023

Yes, you can add text field value to dropdown via button.

Post script you tried.

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
Explorer ,
Oct 24, 2023 Oct 24, 2023

avv98_0-1698215060349.png

I typed 'Try' instead of it showing in the dropdownlist. It shows 'text4' which is the name of the 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 Expert ,
Oct 24, 2023 Oct 24, 2023

Use like this:

 

var drop = this.getField("Dropdown5");
var str = this.getField("text4").valueAsString;
drop.insertItemAt(str,"", 0);

 

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
Explorer ,
Oct 25, 2023 Oct 25, 2023

thank you! it worked

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
Explorer ,
Oct 25, 2023 Oct 25, 2023

hi 😅 can I follow up one last question, what if I wanted the dropdown list to be sorted (Alphabetically) after adding new item in the list what should I change/do?

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 ,
Oct 25, 2023 Oct 25, 2023

Ok, first there was an error in my script above, it's fixed now so use new script.

To sort items, you can use the correct answer from this post:

https://answers.acrobatusers.com/Alphabetic-order-drop-list-q255525.aspx 

When you add document level script, add this line to the script I posted above:

ListEntrySort(drop.name);

Script in button should look like this:

var drop = this.getField("Dropdown5");
var str = this.getField("text4").valueAsString;
drop.insertItemAt(str,"", 0);

ListEntrySort(drop.name);
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 ,
Oct 25, 2023 Oct 25, 2023

Just tick the option under the field's Properties to have it be sorted automatically:

 

try67_0-1698252019872.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 ,
Oct 25, 2023 Oct 25, 2023

@try67 unfortunately, that doesn't work when you add item via button (at least doesn't work for me, or I'm missing something).

If you go to options items are sorted but when using dropdown items are not sorted.

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 ,
Oct 25, 2023 Oct 25, 2023

You're right, it doesn't sort it automatically, and unfortunately this setting is not settable using a script, so it can't be cleared and then re-set. So the only solution is to read the list of current items into an array, add the new item to it, sort that array and then re-apply it using setItems.

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
Explorer ,
Oct 25, 2023 Oct 25, 2023

tnx tnx

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
Contributor ,
Aug 14, 2024 Aug 14, 2024
LATEST

Hi Nesa; I have a similar situation but different in that I would like to delete all the options except the one selected, and also delete all export values by pressing button before requesting signatures. Right now I'm having to do this in prefill stage of sign process. Thanks in advance- kemper

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 ,
Oct 25, 2023 Oct 25, 2023
Try this.
 
//Add list items in the dropdown or list
 
var c = this.getField("List");
c.setItems([[" "],
["day(s)"],
["month(s)"],
]);
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