Plumsail public form - Copy address to new field with toggle button

Warning… I am very new to using Plumsail forms and have never used Java Script before!

I have created a Plumsail Public Form and want to be able to copy a previously entered “Residential” address to the “Company” address field, if the “Copy residential address” toggle button is selected.

The Residential address field names are:

“Streetaddress”, “City”, “State”, “Country” & “Post Code/ZIP”.

The Company address field names are the same just with a “1” afterwards.

I have entered the below JS (copied from Google) and saved the form; however, when I open the form from a link or the preview, enter the “Residential” address information and click on the toggle, nothing happens and the data is not copied to the “Company” address fields.

Please can you help resolve this?

Thanks

Hello and welcome to Plumsail Forms!

I'm sorry your first experience with Javascript started with errors, let's try to fix that.

  1. All modern browsers have handy inspect tools, which you can find by right-clicking and choosing "Inspect" (or something similar). If you open them on the preview page, and open Console tab, you can see additional info that was recorded during code execution (not just yours, any JS code used by a Plumsail form).

    ezgif-5aede114a8ccee09

    I think in this case it would be worth to take a look there, can you check it for any errors, especially for stuff which ends with is undefined?

  2. Have you double-checked that you have a field with Name property of CopyResidentialAddress?

  3. Are you using Multiple Choice field or a Toggle field? Visually they can look the same if for Toggle you set Mode property to Checkbox, but internally they behave differently, and your code seems to be designed for Toggle

  4. I've wanted to share another tip that might be useful when testing JS code later - there is a special function called console.log(), which prints out any value you put inside its brackets to the Console tab. For example, if you replace line 24 with console.log("hello");, every time copyAddress is called, you will see hello printed in console logs. And if you use console.log(fd.field("CopyResidentialAddress").value);, it will print out the current value of this field.

Hi, Thanks for the reply.

  1. I have done what you suggested and can’t see anything that states “is undefined”.

  1. Yes the field name is “CopyResidentialAddress”.

  1. I am using the Toggle field set to “Checkbox”

  1. Ok I will investigate that too.

Any other thoughts?

You've opened the inspect tools, but haven't switched to Console tab. In some browsers (like Edge for example) it might only show tab icons if there isn't enough space:

You can also check for other errors as well, not just "is undefined".

Is this better?

That's perfect! I'm ashamed I haven't seen this myself - you have a typo in fd.rendered, at the very beginning. It should be lowercase, but in your code it's written as

fd.Rendered

That’s brilliant. Thanks very much for your help in solving this. It works now.

It is interesting as I copied that text directly from the Google Ai answer which I think took the code from the Plumsail Help guides.

In any case. Thanks again.