• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

PDF Form - Auto Date Dropdown Field Needs to Default to Today

Explorer ,
Feb 19, 2021 Feb 19, 2021

Copy link to clipboard

Copied

I'm using this code to populate a dropdown with dates 30 days prio to today and 60 days after today. The client would like for Today to be selected by default when the form is opened. I appreciate any help you can give. 

 

var aDateList = [];

var nToday = (new Date).getDate();

var dtItem;

for(var i=-30;i<60;i++)

{

dtItem = new Date;

dtItem.setDate(nToday + i);

aDateList.push(util.printd("mm/dd/yyyy", dtItem));

}

this.getField("DateList").setItems(aDateList);

TOPICS
PDF forms

Views

1.1K

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
community guidelines

correct answers 1 Correct answer

Explorer , Feb 25, 2021 Feb 25, 2021

Thanks for taking the time. My dates as of today are 1/26 - 4/25. I showed the client the latest dropdown with "Please Select" and she is happy with it, When the user clicks it to select it jumps to and selects today's date.

 

Thanks again!

Votes

Translate

Translate
Community Expert ,
Feb 19, 2021 Feb 19, 2021

Copy link to clipboard

Copied

I assume you are using code as document level script?

Add this code at the bottom of your code:

var dt = this.getField("DateList");
dt.value = util.printd("mm/dd/yyyy", new Date());

Votes

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
community guidelines
Explorer ,
Feb 22, 2021 Feb 22, 2021

Copy link to clipboard

Copied

Hi thanks for the reply. I'm using the code in a  PDF form dropdown field. The code is located in Actions OnFocus - Javascript Action. As you mayy have figured out, I'm a newbie who knows almost nothing about Javascript. Would the code you provided go into the code I supplied? If so where.

 

Thanks again.

Votes

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
community guidelines
Explorer ,
Feb 22, 2021 Feb 22, 2021

Copy link to clipboard

Copied

Sorry, I just read the part about putting your code after mine. I did that and today's date is selected when the dropdown is opened. Is it possible to have that selection show t the top of the dropdown when the form is opened forcing the user to scrol up in the dropdown for a previous date?

 

Thanks!

Votes

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
community guidelines
Community Expert ,
Feb 22, 2021 Feb 22, 2021

Copy link to clipboard

Copied

to have today's date to show always in a field as soon as you open the document you may need to call it as a function.

 

In my example I named my function (shown below) "date" .

 

I also call this function as a custom format script from the event target field (which in your case, the event target field would be the dropdown menu).

 

I named my function date(); and this is all you need to add in the custom format script section.

 

Then open the  Documents and Javascripts tool and add this script as the today's date function:

 

function date() {

 

var today = new Date(util.scand("mm/dd/yy", event.target.value));

 

today.setDate(today.getDate());

 

event.value = util.printd("mm/dd/yy", today);

 

}

 

Everytime you open the document the today's date will be displayed in that field.

Votes

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
community guidelines
Explorer ,
Feb 22, 2021 Feb 22, 2021

Copy link to clipboard

Copied

Thank you for the reply. After making the changes below, the field still has the date 30 days in the past showing when the form is opened. When I click on the field, it selects today's date. Perhaps a work around would be to add "Please Select" as the first choice but of course, I don't know how to do that either. Thanks again!

 

I added the function code to the Format tab in the Custom options box. 

function date() {

var today = new Date(util.scand("mm/dd/yy", event.target.value));

today.setDate(today.getDate());

event.value = util.printd("mm/dd/yy", today);

}

 

This code is in the Actions box with OnFocus as the Event.

var aDateList = [];

var nToday = (new Date).getDate();

var dtItem;

for(var i=-30;i<60;i++)

{

dtItem = new Date;

dtItem.setDate(nToday + i);

aDateList.push(util.printd("mm/dd/yyyy", dtItem));

}

this.getField("DateList").setItems(aDateList);
var dt = this.getField("DateList");
dt.value = util.printd("mm/dd/yyyy", new Date());

Votes

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
community guidelines
Explorer ,
Feb 23, 2021 Feb 23, 2021

Copy link to clipboard

Copied

I moved your code (below) to the Document Javascript. Is that the correct spot? I am seeing that today is selected but does not show at the top of the dropdown. Did I need change the form field JavaScript code too?

 

Thanks!

 

function date() {

var today = new Date(util.scand("mm/dd/yy", event.target.value));

today.setDate(today.getDate());

event.value = util.printd("mm/dd/yy", today);

}

Votes

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
community guidelines
Community Expert ,
Feb 23, 2021 Feb 23, 2021

Copy link to clipboard

Copied

I am sorry to tske this long to reply.

 

I wasn't sure of what exactly do you need in the combobox to display.

 

Is it "Please Select" option always visible or the today's date always vusible?

 

Or do you need to have "Please Select" always visible and then underneath that item immediately have today's date as the first option above all othet listed date ranges?

 

You also mentioned that all of the old dates 30 days back still appear listed.

 

Do you want the dropdown menu to remain empty every time this document is opened amd only populate the dates in it when the user clicks on it?

 

In any case, the correct way to move the listed items up or down is by using currentValueIndices or insertItemAt.

 

Can you please elaborate what exactly needs to happen with this field object  when the user opens up the PDF amd what needs to happen if the user just clicks on it , and what needs to happen if the user selects a date from that list.

Votes

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
community guidelines
Explorer ,
Feb 24, 2021 Feb 24, 2021

Copy link to clipboard

Copied

Thanks for the reply.

 

The client would like to have today's date show in the dropdown when the page is opened but still have 30 days in the past and 60 days in the future listed as options. I don't know if that is eve possible so I went with an alternative. 

 

As an alternative, I was able to place "Please Select" as the first option by placing that in the Options list then having the Javascript fill in the dates (I didn't change the original code). When the user clicks the box the date "jumps" to Today's date and highlights it. 

 

I'm not sue which solution would be a better user experience.

 

Thanks again for your valuable assistance!

Votes

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
community guidelines
Community Expert ,
Feb 25, 2021 Feb 25, 2021

Copy link to clipboard

Copied

I am still struggling with that script.

 

I only get 30 dates listed, not 30 in the past and 60 in the future.

Votes

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
community guidelines
Explorer ,
Feb 25, 2021 Feb 25, 2021

Copy link to clipboard

Copied

Thanks for taking the time. My dates as of today are 1/26 - 4/25. I showed the client the latest dropdown with "Please Select" and she is happy with it, When the user clicks it to select it jumps to and selects today's date.

 

Thanks again!

Votes

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
community guidelines
Community Expert ,
Feb 25, 2021 Feb 25, 2021

Copy link to clipboard

Copied

LATEST

Ok great!  And thanks to NesaNurani's guidance as usual.

Votes

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
community guidelines