Fill a Custom Field Automatically With a Short Script
If another tool produces a value on your page, such as a certificate link or a campaign code, you can place it into a form field so it saves with the submission.
Find the field's ID
- Open the form's Preview.
- Right-click the page and choose Inspect.
- Use the pointer tool in the developer panel and click the custom field.
- Copy the value of its
nameandidattributes.
Add the script
- In the builder, drag an HTML element onto the form.
- Click Edit HTML.
- Paste a script like this, replacing
FIELD_IDandmyValuewith your own:
document.getElementsByName('FIELD_ID')[0].value = myValue;
document.getElementsByName('FIELD_ID')[0].dispatchEvent(new Event('input'));
- Save and test.
The second line tells the form that the field changed, so the new value is submitted along with everything else.
Tips
- Make sure the script runs after the value exists and after the form has loaded.
- Confirm the value appears in Sites > Forms > Submissions and on the contact.
- Hidden or read-only fields work well for this.
Remember the HTML editor blocks scripts that read cookies or localStorage.
Good to know: Test with a live submission, not just the preview, since values from outside tools may only exist on the published page.
Was this article helpful?
