Skip to main content
Known Participant
July 5, 2020
Answered

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

  • July 5, 2020
  • 2 replies
  • 3203 views

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. 

 

This topic has been closed for replies.
Correct answer ls_rbls

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.

 

 

2 replies

ls_rbls
ls_rblsCorrect answer
Community Expert
July 6, 2020

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.

 

 

Abbccc96Author
Known Participant
July 7, 2020

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

ls_rbls
Community Expert
July 5, 2020

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.

Abbccc96Author
Known Participant
July 6, 2020

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?

Abbccc96Author
Known Participant
July 6, 2020

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.


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?