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

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

Explorer ,
Oct 24, 2023 Oct 24, 2023

Copy link to clipboard

Copied

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

Views

1.1K

Translate

Translate

Report

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

correct answers 2 Correct answers

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);

 

Votes

Translate

Translate
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(dro
...

Votes

Translate

Translate
Community Expert ,
Oct 24, 2023 Oct 24, 2023

Copy link to clipboard

Copied

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;

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Post script you tried.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Use like this:

 

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

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

thank you! it worked

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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);

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

 

try67_0-1698252019872.png

 

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

@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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

tnx tnx

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Try this.
 
//Add list items in the dropdown or list
 
var c = this.getField("List");
c.setItems([[" "],
["day(s)"],
["month(s)"],
]);

Votes

Translate

Translate

Report

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