Populate dropdown with year values on document load

New Here ,
Apr 26, 2018

Copy link to clipboard

Copied

Hey Everyone,

Hoping someone can help me with this as I'm new Acrobat forms. I'm trying to populate dropdown list values in an adobe acrobat form via Javascript. Basically looking to populate it with the Current Year, and two years after it. So option values of 2018, 2019, 2020. I'm not sure how to achieve this where the dropdown is populated when the PDF is opened. Would this be done at the document level javascript ? And if so how is this initiated ?

This is what I have so far (Not sure if it's correct):

var d = new Date();

var y = d.getFullYear();

Dropdownlist1.setItems([y, y + 1, y + 2]);

Any help would be appreciated, thanks.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Change the last line to:

this.getField("Dropdownlist1").setItems([y, y + 1, y + 2]); 

Regarding the location of the code: That depends on when you want it to update the list.

Also, realize that when you run it the current value of the field is discarded, so running it each time the file is opened is probably not a good idea...

TOPICS
Acrobat SDK and JavaScript, Windows

Views

70

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

Populate dropdown with year values on document load

New Here ,
Apr 26, 2018

Copy link to clipboard

Copied

Hey Everyone,

Hoping someone can help me with this as I'm new Acrobat forms. I'm trying to populate dropdown list values in an adobe acrobat form via Javascript. Basically looking to populate it with the Current Year, and two years after it. So option values of 2018, 2019, 2020. I'm not sure how to achieve this where the dropdown is populated when the PDF is opened. Would this be done at the document level javascript ? And if so how is this initiated ?

This is what I have so far (Not sure if it's correct):

var d = new Date();

var y = d.getFullYear();

Dropdownlist1.setItems([y, y + 1, y + 2]);

Any help would be appreciated, thanks.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

Change the last line to:

this.getField("Dropdownlist1").setItems([y, y + 1, y + 2]); 

Regarding the location of the code: That depends on when you want it to update the list.

Also, realize that when you run it the current value of the field is discarded, so running it each time the file is opened is probably not a good idea...

TOPICS
Acrobat SDK and JavaScript, Windows

Views

71

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
Apr 26, 2018 0
Most Valuable Participant ,
Apr 26, 2018

Copy link to clipboard

Copied

Change the last line to:

this.getField("Dropdownlist1").setItems([y, y + 1, y + 2]); 

Regarding the location of the code: That depends on when you want it to update the list.

Also, realize that when you run it the current value of the field is discarded, so running it each time the file is opened is probably not a good idea...

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...
Apr 26, 2018 0
New Here ,
Apr 26, 2018

Copy link to clipboard

Copied

Thanks try67, that worked! For now I'm running it on the MouseDown action which I think should be sufficient. Although if I wanted to initiate the trigger on document level javascript. How would I go about setting that up ?

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...
Apr 26, 2018 0
Most Valuable Participant ,
Apr 26, 2018

Copy link to clipboard

Copied

Use MouseUp instead of MouseDown. It's a more standard way of doing it.

You can place the code under Tools - JavaScript - Document JavaScripts to have it execute when the file is opened.

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...
Apr 26, 2018 0
Heliosoz LATEST
New Here ,
Apr 26, 2018

Copy link to clipboard

Copied

Appreciate it thank you, I think I have to resort to one of the field events instead as you mentioned that having it on the document level will discard the field value every time the file is reopened.

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...
Apr 26, 2018 0