Skip to main content
Klook
Participating Frequently
February 22, 2023
Answered

Automatically add predefined glyphs to a dropdown list?

  • February 22, 2023
  • 1 reply
  • 1627 views

Hi there

 

The setup: in a PDF I have a bunch of dropdown lists with a huge amount of entries each. The selected items (input) of all boxes populates into one text field (output).

 

What I'm trying to achieve: I need the output to be automatically separated by ", " (comma/space)
so it will look like this (box selection 1, box selection 2, ...). Since there's a lot of entries, I can't do that manually. I think ther's a simple Javascript solution to add to the dropdowns or the text field, but I have no clue.

Can someone help please? Thanks in advance!

This topic has been closed for replies.
Correct answer try67

Thanks so much!! Ok, one last question, then I'll leave you alone 😅 (sorry, just stumbled upon this):
is it also possible to exclude the adding of the ", " I asked originally for some specific dropdowns? Also for some reason the commas stay in the output field when I hit the reset button I put on the document...

Thanks A LOT!!!


This requires a different approach... Generally speaking, you should specify all of these conditions at the start of the development process, not half-way through it. Anyway, here you go:

 

var dropdowns = ["Dropdown1", "Dropdown2", "Dropdown3", "Dropdown4"]; // etc.
var values = [];
var msg = "";
for (var i in dropdowns) {
	var fname = dropdowns[i];
	var v = this.getField(fname).valueAsString;
	if (v!="") {
		if (msg!="" && fname!="Dropdown1" && fname!="Dropdown4") msg+=", "; // add a comma, except before certain values, or at the start of the string
		
		if (fname=="Dropdown2") msg+="Some text before Dropdown2: " + v; // add specific text before this value
		else if (fname=="Dropdown3") msg+="Some text before Dropdown3: " + v; // add specific text before this value
		else msg+=v; // add the field's value as is to the string
	}
}
event.value = msg;

1 reply

try67
Community Expert
Community Expert
February 22, 2023

Do these drop-downs have a default value you want to ignore, or just add them all up, no matter what value is selected in them?

Klook
KlookAuthor
Participating Frequently
February 22, 2023

Wow, that was fast, thank you!
Not sure if I get your question right, but I guess just add them all up. Here's an example:

Dropdown 1:
Entry A (user selects A)
Entry B
Entry C

Dropdown 2:
Entry D 
Entry E (user selects E)
Entry F

Desired output to text field (adding these selected entries already works, but it's not separated by ", "):
Entry A, Entry E

Appreciate your help!

try67
Community Expert
Community Expert
February 22, 2023

Then you can use this code as the custom calculation script of the text field where you want the combined text to be shown:

 

var dropdowns = ["Dropdown 1", "Dropdown 2"]; // etc.
var values = [];
for (var i in dropdowns) {
	var v = this.getField(dropdowns[i]).valueAsString;
	values.push(v);	
}
event.value = values.join(", ");