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

Automatically fill date field when a check-box is ticked

Explorer ,
Nov 28, 2024 Nov 28, 2024

Copy link to clipboard

Copied

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.

TOPICS
PDF forms

Views

961

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

Community Expert , Nov 29, 2024 Nov 29, 2024

- You had the Date format still selected under the text field.

- You had an invalid script under the text field's Validation event.

- You had the wrong date format in the code of the check-box.

- The code of the check-box was only set to update the field when it was empty, so clicking it did nothing once that was the case.

- The code of the check-box did not clear the text field when you un-checked it.

Votes

Translate

Translate
Explorer ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

Also when I put in the second script and remove the first script, having changed the checkbox to Date Trigger, the checkbox doesn't fill in the date field at all. Even with no format on the date 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
Community Expert ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

You don't need to rename the field or move it anywhere. Just make sure the correct field name appears in the code.

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 ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

Ah, I think it may not have worked because there are full stops after Date and Date Trigger in the script. I've taken them out and the date box fills again but now it's gone back to American format and not emptying the date box if the check box is emptied! Literally, all I took out was the full stop! 😞 😞 

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 ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

Make sure you enable the option to show the JS Console on errors and messages (via Preferences - JavaScript). That will immediately show you when you have errors in your code when you run it.

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 ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

Great, thanks - I have that selected now.

 

Am I posting the new "var" script in the wrong place? I'm putting it in the checkbox script bit.

 

I watched the video on the article you linked to but that's about calculations so I got confused again. I need to create individual duplicates of all the fields for about 40 pages. I've created ones for the name on each page which is no problem - I just duplicated them across each page - but they will always have the same values. Whereas the other text boxes will have unique values. I can't figure out where to put the script in for these boxes as the script in the checkbox is an action in response to a mouse click.

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 ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

Sorry, me again - I'm trying to create multiple unique copies of those fields following the article writer's earlier article about doing just this. It won't let me create cells which go "down" but only duplicates across multiple pages which means that the contents of the field also gets duplicated. The article says " Open the field properties and put the number 1 as the Export Value in the Options tab" but I don't have any export value in the Options tab. 

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 ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

What are you trying to do:

1)  Create one check box that fills in multiple date fields?

2)  Create multiple checkbox/date field pairs where each check box fills in the date for a different text box?

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 ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

2. I'm sorry to ask so many questions. other text boxes/dropdowns are contained but these 2 are the only ones which need automating. They are all repeated on each page.

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 ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

You're still not answering my question.

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 ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

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

 

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 ,
Nov 29, 2024 Nov 29, 2024

Copy link to clipboard

Copied

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.

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 ,
Nov 30, 2024 Nov 30, 2024

Copy link to clipboard

Copied

I'm so sorry, I missed the video you had uploaded. That works perfectly now, and I have managed to change the date format. Thank you very much indeed for taking the time to do that. I thought that I had to have both scripts in which is what was getting me confused.

 

Using the article you linked to, I've also been able to create 3 'sum' boxes at the bottom of each page.

 

To create multiple copies of these I would just copy and paste them onto different pages, as you say - and 'create multiple copies' means the formula would move onto the different pages I hope?

 

I'm really grateful for your help - never done anything like this before and in a new role so really want to do a good job.

 

At the bottom of the document, I have boxes which woud

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 ,
Nov 30, 2024 Nov 30, 2024

Copy link to clipboard

Copied

When you create multiple copies it creates fields with all the same properties (including scripts) as the original, except for the field name, which it adds a numbered suffix to.

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 ,
Nov 30, 2024 Nov 30, 2024

Copy link to clipboard

Copied

LATEST

It looks like if I do "duplicate across pages" then the fields aren't unique though, so I will need to copy and paste onto each page, as you say. 

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