Skip to main content
Known Participant
October 25, 2023
Answered

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

  • October 25, 2023
  • 2 replies
  • 2611 views

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

This topic has been closed for replies.
Correct answer Nesa Nurani

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?


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

2 replies

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

 

avv98Author
Known Participant
October 25, 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

 

Nesa Nurani
Community Expert
Community Expert
October 25, 2023

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

Post script you tried.