Copy field values in Acrobat using Javascript
Asked Answered
K

2

9

How can I copy the form field values from one set of fields to another using javascript.

The idea here is to have a 'use shipping/billing address' type of button that copies the user information from one block of fields to another identical set of fields.

Right now, I call an action upon click of a button to execute the following javascript:

this.field1.value = this.field2.value;

However that action yields an 'undefined' error in the debugger.

Kkt answered 8/11, 2011 at 19:34 Comment(2)
That looks right. Can you paste more/all of the code so we can look for an inconsistency elsewhere?Sorghum
That's all I've got. It just runs this code as an action when a button is clicked.Kkt
K
15

For posterity, this is the solution to the problem:

getField("field2").value = getField("field1").valueAsString;

Also, note that field2 is set to field1 so the order is backwards.

Kkt answered 11/11, 2011 at 21:11 Comment(0)
F
5

I used the following code to avoid overwriting the value in the second field if it has something in it already:

//Set the source and destination vars:
      var source = this.getField("Box1");
      var destination = this.getField("Box2");

//See if destination is empty and if so, insert source value
      if(destination.value==''||destination.value==null){destination.value=source.value}

I used it on "On Blur" of the source Field, but you could use a button with "Mouse Up" as the trigger. (I found the code on this website. It includes more complicated options for populating multiple fields or even joining values from two source fields into one destination field.)

Firth answered 6/5, 2015 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.