How to auto add values from text fields to Combo Box/Dropdown list?

Community Beginner ,
Jul 05, 2020

Copy link to clipboard

Copied

On my PDF I have three Email Address text fields - when a value is entered into one of these fields I would like the entered value to automatically be added to a combo box to allow the user to select which email address they would like to use to receive correspondence. 

 

So far I have the following code added to the combo box: 

 

 

var emailList = [" "];

if(this.getField("EmailAddress1").value) {
emailList.push([this.getField("EmailAddress1").value]);
}

if(this.getField("EmailAddress2").value) {
emailList.push([this.getField("EmailAddress2").value]);
}

if(this.getField("EmailAddress3").value) {
emailList.push([this.getField("EmailAddress3").value]);
}

this.getField("PortfolioCorrespondenceEmail1").clearItems();
this.getField("PortfolioCorrespondenceEmail1").setItems(emailList);

 

 

 

 

This code does return the values as hoped, however, I am required to click in the dropdown to refresh the values in the list, this may give the impression to users that there are no emails available for selection as initially there is just a " " option until the values are loaded into the list.

 

I have selected "commit selected value immediately" but it doesn't seem to fix the problem. At the moment I have no code in the custom keystroke, I am wondering whether there is something I should add in there?

 

Update: I have since moved the code into the custom calculation area and the dropdown list options are populating immediately, however it won't let me select one of the options. When I select one of the options it just looks like the list is reloading and the option doesn't select. 

 

Adobe Community Professional
Correct answer by ls_rbls | Adobe Community Professional

Honestly this is all you need:

 

var f = event.target;
var emailList = [event.value,""];

if(this.getField("a").value) {emailList.push([this.getField("a").value]);} 

if(this.getField("b").value) {emailList.push([this.getField("b").value]);} 

if(this.getField("c").value) {emailList.push([this.getField("c").value]);}


f.setItems(emailList);

 

You can run it as custom calculation script and also as custom format script.

 

It updates on real-time and if the user deletes the other two email and decides to delete the third entry, clicking on the empty item of the drop down menu clears the box.

 

 

TOPICS
Acrobat SDK and JavaScript, Create PDFs, Edit and convert PDFs, PDF forms

Views

129

Likes

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

How to auto add values from text fields to Combo Box/Dropdown list?

Community Beginner ,
Jul 05, 2020

Copy link to clipboard

Copied

On my PDF I have three Email Address text fields - when a value is entered into one of these fields I would like the entered value to automatically be added to a combo box to allow the user to select which email address they would like to use to receive correspondence. 

 

So far I have the following code added to the combo box: 

 

 

var emailList = [" "];

if(this.getField("EmailAddress1").value) {
emailList.push([this.getField("EmailAddress1").value]);
}

if(this.getField("EmailAddress2").value) {
emailList.push([this.getField("EmailAddress2").value]);
}

if(this.getField("EmailAddress3").value) {
emailList.push([this.getField("EmailAddress3").value]);
}

this.getField("PortfolioCorrespondenceEmail1").clearItems();
this.getField("PortfolioCorrespondenceEmail1").setItems(emailList);

 

 

 

 

This code does return the values as hoped, however, I am required to click in the dropdown to refresh the values in the list, this may give the impression to users that there are no emails available for selection as initially there is just a " " option until the values are loaded into the list.

 

I have selected "commit selected value immediately" but it doesn't seem to fix the problem. At the moment I have no code in the custom keystroke, I am wondering whether there is something I should add in there?

 

Update: I have since moved the code into the custom calculation area and the dropdown list options are populating immediately, however it won't let me select one of the options. When I select one of the options it just looks like the list is reloading and the option doesn't select. 

 

Adobe Community Professional
Correct answer by ls_rbls | Adobe Community Professional

Honestly this is all you need:

 

var f = event.target;
var emailList = [event.value,""];

if(this.getField("a").value) {emailList.push([this.getField("a").value]);} 

if(this.getField("b").value) {emailList.push([this.getField("b").value]);} 

if(this.getField("c").value) {emailList.push([this.getField("c").value]);}


f.setItems(emailList);

 

You can run it as custom calculation script and also as custom format script.

 

It updates on real-time and if the user deletes the other two email and decides to delete the third entry, clicking on the empty item of the drop down menu clears the box.

 

 

TOPICS
Acrobat SDK and JavaScript, Create PDFs, Edit and convert PDFs, PDF forms

Views

130

Likes

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
Jul 05, 2020 0
Adobe Community Professional ,
Jul 05, 2020

Copy link to clipboard

Copied

Drop the ("") quotes inside the brackets.

 

And use code like this: 

 

var f = event.target;
var emailList = [];

if (!event.willCommit) {

if(this.getField("EmailAddress1").value) {emailList.push([this.getField("EmailAddress1").value]);} 

if(this.getField("EmailAddress2").value) {emailList.push([this.getField("EmailAddress2").value]);} 

if(this.getField("EmailAddress3").value) {emailList.push([this.getField("EmailAddress3").value]);}

f.setItems(emailList);

} 

 

Copy the code above and paste it in the Custom Fomat Script section  instead of Custom Key Stroke or custom calulation script.

Likes

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
Reply
Loading...
Jul 05, 2020 0
Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

Thanks for the response, I am getting the same problem. When I select the item in the drop down it doesn't stay selected. It just reloads the list and selects the first item in the array as the value. However, it does auto update the value, just need to to sort out the value selection side of it working now so the value persists. Do you have any idea on how to fix this?

Likes

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
Reply
Loading...
Jul 06, 2020 0
Participant ,
Jul 06, 2020

Copy link to clipboard

Copied

Hi,

I would suggest to write in validation script of the 3 e-mail address fields:

if (event.value==null || event.value=="") {
event.rc=true;
} else {
if (event.value.match(/\S+@\S+\.\S{2,}/)==null) {
app.alert("The input doesn\'t suit to the syntax required for an email address.\rPlease modify, example: firstname.lastname@supplyer.com");
event.rc=false;
this.resetForm(event.target.name);
} else {
event.rc=true;
this.getField("PortfolioCorrespondenceEmail1").insertItemAt(event.value,-1);
}
}

That will add the new e-mail address at the top of the combo box.

You can improve the script to check if the e-mail address already exist or to sort the combo box...

@+

Likes

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
Reply
Loading...
Jul 06, 2020 2
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

if you placed the code above as a custom format script, it autoupdates the list automatically  as soon as the user clicks on the dropdown arrow.

 

It will also populate the the combobox in whatever order the email fields are fulled in, which is also in real-time.

 

If the user skips the email1 and filled in email 2 and email3 , then email 2 will appear first (on top of the list) and followed below it by email3.

 

If you're getting the same error that means tbat you're still trying to use the script as a custom calculating script, or custom keystroke script.

 

The script above is very simple and it works.

 

Please confirm that you haven't change or added anything to it, and where did you place the script.

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

You also didn't remove the quotes from inside the brackets.  That is why you're getting that problem.

 

But I see what you're saying.

 

You want the list of emails to not  display any value automatically in that field until the user actually clicks on a selection of the refreshed populated list.

Likes

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
Reply
Loading...
Jul 06, 2020 0
Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

Hi, once again, thanks for taking the time to respond. I have copied and pasted your code into the Custom Format area, there is no extra code in the Calculate/Keystroke or "Run a Javascript" fields. As you highlighted, the reason I am adding the " " is so that the initial value appears to be empty until the user clicks the dropdown and selects a value. So with your code, I have achieved the automatic update of the dropdown list values, however, it doesn't allow me to select a value other than the first one in the array. For example, I have three email addresses in the dropdown, no matter which one I click from the list, the first is always selected. Do you know how I could fix this to allow them to select any of the values?

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

strange. that is not happening on my end. It only happens if the quotes are included inside the brackets.

 

 

I'm working on it . I will reply back with some modifications to the script that is partially working

Likes

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
Reply
Loading...
Jul 06, 2020 0
Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

Thanks again, even without the " " inside the brackets, I am getting the same result. It appears to be selected but once I click outside the field it resets the value. It's annoying because it's so close to working!

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

Does you use any calculation at the dropdown list?

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

Are you sure the script is in the "Custom Format Script" section?

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

As mentioned by Bernd, the script that I sahred should be in the "Custom Format Script" of the dropdown menu field.

Likes

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
Reply
Loading...
Jul 06, 2020 0
Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

Yep, 100% in the Custom Format Script. No logic in the Calculate or Keystroke. I have tried to include a GIF of whats happening below, I am not sure if it will load correctly but thought it was worth a go, this shows whats happening and where the code is. 

 

And I've triple checked that there isn't any conflicting code in any other fields that could be effecting the outcome. I just created a fresh new PDF with 3 text fields and 1 dropdown to test the code on that and it didn't work for me there either. Would it be possible for you to send over a blank PDF where it is working for you and I can check them side by side. 

 

@Bernd_Alheit The only code that is on the dropdown/related fields is entered into the Custom Format Script area. So no calculation is being used at the dropdown list. 

 

 

 

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

I know what the problem is.

 

In my script posted above the bracket look like this [ ]  with a space in between, but when you copy and paste it in your editor it will become like this [] with no space.

 

Hit the space bar one time to add that space in-between the brackets and you should see the script working immediately.

Likes

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
Reply
Loading...
Jul 06, 2020 0
Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

Added a space between the brackets at line: 

var emailList = [];

 but the outcome is still the same, unfortunately. It's a strange one this as I do have the code exactly as you provided (now with the updated space) but it's not working for me and is for you.

 

If you have the time, is there a way I can send you this file and you can see if I'm being a numpty somewhere? I believe I have followed the instructions you have given by taking an exact copy and paste of the code and entering it into the Custom Format Script on the drop-down with no code running elsewhere. 

 

Also, did the GIF work for you? (Its clearer if you open it in a new window, it shows how I have set up the code in the dropdown). 

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

Sure, just click on the "Share" button and it will generate a link that you can copy and share it here in this thread so all the parties trying to help can evaluate it, or if you feel more comfortable send it as a private message (either way is fine). 

Likes

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
Reply
Loading...
Jul 06, 2020 0
Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

Here is the link: 

 

https://www.dropbox.com/s/upks79rmajx4w1k/ComboBoxTest.pdf?dl=0

 

I will add it to the main post as well. Let me know if there are any issues with it. 

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

Don't use the script as custom format.

You must use validation scripts at the 3 text fields.

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

Hey Bernd,

 

I think that the validation script that bebarth posted is actually spot on.

 

I moved the original script to custom calculation script and it worked, and now I am stuck. It seems like the script breaks the way it is coded. 

 

I also tried  adding a read only hidden field with one empty space on it and for a moment it was populating great, then back to the same issue that Abbccc96 was observing.

Likes

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
Reply
Loading...
Jul 06, 2020 0
Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

bebarth's code does add to the dropdown and allow selection, however, the items are added to the top of the dropdown through the use of "-1" at the index. Is there a value I could change this to so the email was added to the end of the dropdown list?

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

The beauty of the little code that you posted is that is almost doing its job the way is supposed to work.

 

The validation script that bebarth shared also do its job as mentioned, but as I'm testing it also discovered that it can get you stuck in an popup alert infinite loop if the user doesn't type in the email correct format as declared by the validation script.

 

 

Likes

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
Reply
Loading...
Jul 06, 2020 0
Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

I've been playing around with bebarths code and got to the following: 

if (event.value==null || event.value=="") 
{
event.rc=true;

} else {
if (event.value.match(/\S+@\S+\.\S{2,}/)==null) {
app.alert("The input doesn\'t suit to the syntax required for an email address.\rPlease modify, example: firstname.lastname@supplyer.com");
event.rc=false;
this.resetForm(event.target.name);
} else {
event.rc=true;
var arr = [" ", event.value];

if(this.getField("a").value) {
arr.push(this.getField("a").value);
}
if(this.getField("b").value) {
arr.push(this.getField("b").value);
}

this.getField("dropdown").clearItems();
this.getField("dropdown").setItems(arr)
}
}

Now it is in a state where the dropdown list is getting updated automatically and allowing me to select a value from the list successfully. However, if one of the values in one of the email address text boxes is removed then it doesn't automatically get removed from the list so I am guessing I just need to have a play around with the first if statement of the script to include some logic to attempt to remove the value from the list if empty. 

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

and where are you using the original script?

Likes

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
Reply
Loading...
Jul 06, 2020 0
Community Beginner ,
Jul 06, 2020

Copy link to clipboard

Copied

I believe to have it working as hoped now, added logic into the first if statement now, so if this certain email address field is null, then it will just set the combo box values as the values from the other two fields if they contain a value, if not no values will be set as hoped. Going to aim to tidy the code up a bit though as it's late now I've just done a rough sketch of it. Could you tell me more about how you got into the infinite loop using this validation? I am debating removing the email address format validation to remove the risk of that. 

 

if (event.value==null || event.value=="") 
{
event.rc=true;
var arr2 = [" "]

if(this.getField("b").value) {
arr2.push(this.getField("b").value);
}
if(this.getField("c").value) {
arr2.push(this.getField("c").value);
}
this.getField("dropdown").clearItems();
this.getField("dropdown").setItems(arr2)

} else {
if (event.value.match(/\S+@\S+\.\S{2,}/)==null) {
app.alert("The input doesn\'t suit to the syntax required for an email address.\rPlease modify, example: firstname.lastname@supplyer.com");
event.rc=false;
this.resetForm(event.target.name);
} else {
event.rc=true;
var arr = [" ", event.value];

if(this.getField("b").value) {
arr.push(this.getField("b").value);
}
if(this.getField("c").value) {
arr.push(this.getField("c").value);
}

this.getField("dropdown").clearItems();
this.getField("dropdown").setItems(arr)
}
}

 

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

I got it working too as expected with the original code.

 

There's no need to use a validation script and you can oplace this script as Custom Calculation Script.

 

Honestly everyone here contributed with solving little bits and pieces of your code puzzle.

 

Here it is:

 

var f = event.target;
var emailList = [event.value, ""];

if (!event.willCommit) {

if(this.getField("a").value) {emailList.push([this.getField("a").value]);} 

if(this.getField("b").value) {emailList.push([this.getField("b").value]);} 

if(this.getField("c").value) {emailList.push([this.getField("c").value]);}

this.getField("dropdown").clearItems();

f.setItems(emailList);

} 

Likes

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
Reply
Loading...
Jul 06, 2020 0
Adobe Community Professional ,
Jul 06, 2020

Copy link to clipboard

Copied

Honestly this is all you need:

 

var f = event.target;
var emailList = [event.value,""];

if(this.getField("a").value) {emailList.push([this.getField("a").value]);} 

if(this.getField("b").value) {emailList.push([this.getField("b").value]);} 

if(this.getField("c").value) {emailList.push([this.getField("c").value]);}


f.setItems(emailList);

 

You can run it as custom calculation script and also as custom format script.

 

It updates on real-time and if the user deletes the other two email and decides to delete the third entry, clicking on the empty item of the drop down menu clears the box.

 

 

Likes

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
Reply
Loading...
Jul 06, 2020 0
Abbccc96 LATEST
Community Beginner ,
Jul 07, 2020

Copy link to clipboard

Copied

Thanks for the help guys. Got it working as hoped now, appreciate the time you spent helping me. 

Likes

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
Reply
Loading...
Jul 07, 2020 1