Skip to main content
Known Participant
November 28, 2024
Question

Automatically fill date field when a check-box is ticked

  • November 28, 2024
  • 1 reply
  • 6522 views

I have created a form which will be filled in over several days. When a checkbox is ticked, I want the date field next to it to automatically fill in with today's date. I don't want this entry to update when the date changes; it needs to record when that particular box was ticked.

Please could you help me write a script to help this occur? I have to then cover about 40 pages with the same fields, so would like to get it right before I go on a copying session! Thanks very much.

This topic has been closed for replies.

1 reply

PDF Automation Station
Community Expert
Community Expert
November 29, 2024

Enter the following mouse up action script in the check box (assuming the text field name is Date):

if(!this.getField("Date").value && event.target.value!="Off")

{this.getField("Date").value = util.printd("mm/dd/yyyy", new Date());}

The blue text ensures the date will not update after it has already been entered.  If you want it to update every time the box is checked, remove the blue text from the script.

Known Participant
November 29, 2024

Thanks so much! It looks like that has worked. Is there a quick way of duplicating both the check box and date field 6 times on 40-odd pages? Will it update the field names automatically in the Javascript? I know how to do multiple copies, etc.

PDF Automation Station
Community Expert
Community Expert
November 29, 2024

I wrote 2 - as in, trying to create option 2

 


The script is always a mouse up action in the check box.  If you are entering the scripts one-at-a-time you have to modify the field name in the script to match the field name that the script is populating.  DO NOT format the text fields as the script will push the correctly formatted value into the field.  My original script did not clear the date when the check box is unchecked because you didn't ask for that, but I see you want that by reading through this thread.  The script should be:

 

 

if(event.target.value=="Off")
{this.getField("Date").value = ""}
else
{this.getField("Date").value = util.printd("mm/dd/yyyy", new Date());}

 

 

Modify the date format to the format you want (eg. "dd/mm/yyyy" or "mmmm dd, yy", etc).  You can then copy and paste the script into all of the check boxes but you will have to modify the 2 "Date" words in the example to match the actually field names (eg. Date1, Date2, etc).  You asked about a shortcut to create multiple copies of these pairs (check boxes and corresponding text fields that are populated with a date from the corresponding check box).  You can write a script that will anticipate the field names when using right-click > Create multiple copies so you only have to write one script.  I provided an article (with video), a sample script, and a video of how to do this.  If your check box is called Field Trigger (it can be called whatever you want but I have to call it something for the example), it will change to Field Trigger.0 when you create multiple copies.  If you create 10 copies down and none across the names will be Field Trigger.0 through Field Trigger.9.  If your field is called Date and you do the same thing, your fields will be called Date.0 through Date.9.  You can write ONE script that works for all of these by extracting the number from the field name then adding it back in.  Here's the script for this example:

 

var num=event.target.name.replace("Field Trigger.", "");
if(event.target.value=="Off")
{this.getField("Date."+num).value=""}
else
{this.getField("Date."+num).value = util.printd("mm/dd/yyyy", new Date());}

 

If your fields are named Check Box.0 through Check Box.9 you would change the first line to

var num=event.target.name.replace("Check Box.", "");

Once you have your 10 pairs, or however many you need, simply move them to where they belong on the form.  If they are on another page you can cut and paste them by selecting them, pressing Ctrl + x, navigating to the correct page, and pressing Ctrl +v.