Skip to main content
June 25, 2020
Answered

Autofill today's date using a dropdown menu

  • June 25, 2020
  • 1 reply
  • 2358 views

I am typing up a form where, when a name is selected from a dropdown menu list in one field named "Name", the field directly below it autopopulates with the date (the field is named "Date". This document will have several sections with these two fields, so when different parts of the form are filled out, the reader will know who filled it out and on what day. Currently, I have this written out under the calculations tab for "Date":

var D = this.getField("Name").valueAsString;
if (D=="Donald") event.value = util.printd("mm/dd/yyyy",new Date);

var K = this.getField("Name").valueAsString;
if (K=="Karin") event.value = util.printd("mm/dd/yyyy",new Date);

var T = this.getField("Name").valueAsString;
if (T=="Tom") event.value = util.printd("mm/dd/yyyy",new Date);

This seemed to work, leaving the "Date" fields filled with the current date when a name was selected. However, the next day, when the next dropdown menu was filled out, all previous dates updated to match the new date. Is there any way to prevent this?

 

This topic has been closed for replies.
Correct answer try67

What's the point of these conditions if you just populate the field anyway? Or do you only want to populate it if specific names are selected from the list?

Also, your code is a bit cumbersome and repeats certain command unnecessarily. You can replace it with this code (which will also only populate the field if it's empty):

 

var name = this.getField("Name").valueAsString;
if (event.value=="" && (name=="Donald" || name=="Karin" || name=="Tom")) event.value = util.printd("mm/dd/yyyy",new Date);

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 25, 2020

What's the point of these conditions if you just populate the field anyway? Or do you only want to populate it if specific names are selected from the list?

Also, your code is a bit cumbersome and repeats certain command unnecessarily. You can replace it with this code (which will also only populate the field if it's empty):

 

var name = this.getField("Name").valueAsString;
if (event.value=="" && (name=="Donald" || name=="Karin" || name=="Tom")) event.value = util.printd("mm/dd/yyyy",new Date);
June 26, 2020

My employer, who was requesting the form, wanted these specific conditions (even though they are redundant in a way), since there will be a large number of fields to fill out and having it automatically populate based off of these specific names would save on time. Thank you so much, I am not used to Javascript, so your answer definitely helps a lot.