Copy link to clipboard
Copied
I am trying to find a solution to make fillable PDF forms friendlier for my customers, and would like to have a solution that if they check a box as true, the following fields information would be feed by same information of above fields, so that they would avoid typing the same information more than once.
Is there a possibility to create such JavaScript or function?
Copy link to clipboard
Copied
Yes, that's possible. However, do you want the users to be able to overwrite this information when the check-box is ticked, or should they always have the same values as the "original" fields in this scenario?
Copy link to clipboard
Copied
Thank you for a prompt reply!
I want that information appears in the fields after the checkbox is ticked (first option of your question), if it is not ticked, the fields have to remain without any data.
Copy link to clipboard
Copied
Please read my question more carefully. You did not answer it.
Copy link to clipboard
Copied
Yes, apologies. As original value.
Copy link to clipboard
Copied
Again, not what I asked. Let's try again: Do you want the users to be able to edit the fields where the values are copied to when the check-box is ticked? In other words, should it "lock" them which the check-box is ticked, or should it just copy the values at the moment you tick it, and that's it?
Copy link to clipboard
Copied
It should copy the values at the moment checkbox is ticked.
Well, the logic should be like - if (checkbox is true then value.field2 = value.field1 else empty)
Copy link to clipboard
Copied
As the MouseUp event of the check-box add the following code:
this.getField("field2").value = (event.target.value=="Off") ? "" : this.getField("field1").valueAsString;
Adjust the field names as needed.
Copy link to clipboard
Copied
Great! It worked!
Could you please additionally advice how the same condition should be applied when there are more than one field that should contain an original value? Should the same condition be written to all the fields separately adjusting only the names of the fields?
Copy link to clipboard
Copied
Yeah, you can just duplicate that line, adjusting the field name in each case.
If you have a lot of fields for which you want to do this I can suggest a more efficient alternative, though.
Copy link to clipboard
Copied
There are 7 fields for the same rule. Perhaps alternative way would be better.
Copy link to clipboard
Copied
Use this:
var fields = ["field2", "field3", "field4"]; // enter actual field names here
for (var i in fields) {
var f = this.getField(fields[i]);
f.value = (event.target.value=="Off") ? "" : this.getField("field1").valueAsString;
}
Copy link to clipboard
Copied
Thank you very much for your support! The first script with several repetitions works perfectly. However, the alternative script hasn't worked properly as it gives me the same value to 7 fields; and I have tried to do several modifications to the values in brackets.
Copy link to clipboard
Copied
Sorry, my mistake. Here's the correct code:
var sourceFields = ["field1", "field2", "field3"]; // the field names from which to copy the values
var targetFields = ["field4", "field5", "field6"]; // the field names to which to paste the values
for (var i=0; i<sourceFields.length; i++) {
var targetField = this.getField(targetFields[i]);
var sourceField = this.getField(sourceFields[i]);
targetField.value = (event.target.value=="Off") ? "" : sourceField.valueAsString;
}
Copy link to clipboard
Copied
Now it works!
Thank you for your time and support!
Copy link to clipboard
Copied
Hi, I'm trying to use the same code, but I get a syntax error at line 2 message. What can I do to fix it?
var sourceFields = [“RBCP.0”, “RegStartTime.1.0”, “RegEndTime.1.0”, “RegInstructionalMinutes.1.0”];
var targetFields = [“StCP1”, “StPrRegStartTime1”, “StPrRegEndTime1”, “StPrRegInstructionalMinutes.1”];
for (var i=0; i<sourceFields.length; i++) {
var targetField = this.getField(targetFields[i]);
var sourceField = this.getField(sourceFields[i]);
targetField.value = (event.target.value=="Off") ? "" : sourceField.valueAsString;
}
Copy link to clipboard
Copied
You must only use straight quotes (like this: " ), not curly ones (like this: “ ” ).
Copy link to clipboard
Copied
Thank you!
Copy link to clipboard
Copied
This problem with quotes mostly happen if Word (or similar) is used to compose or collect scripts. Best just not to do that. Use Notepad if you're in Windows, to keep the text unchanged.