Skip to main content
Klook
Participating Frequently
Feb 22, 2023 8:59 AM UTC
Answered

Automatically add predefined glyphs to a dropdown list?

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Feb 26, 2023 10:34 AM UTC

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;

10 replies

try67
Community Expert
Community Expert
Feb 22, 2023 9:02 AM UTC

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?

Translate
Klook
KlookAuthor
Participating Frequently
Feb 22, 2023 9:07 AM UTC

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!

Translate
try67
Community Expert
Community Expert
Feb 22, 2023 9:19 AM UTC

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(", ");
Translate