CORE MODULE

Add JavaScript to a Form

What you'll learn in this guide

How to add custom JavaScript to a form template in CX Platform — for client-side validation, UI behaviour, analytics tracking, and other form-specific scripting that runs after the page has loaded.

Overview

The Custom Javascript field lets you attach form-specific JavaScript to a template. Your code is injected inside a <script> tag at the bottom of the HTML page, after the page content has loaded — so it has access to the rendered form elements when it runs.

This is the right place for lightweight client-side logic that's specific to one form: custom validation rules, show/hide behaviour that goes beyond what Form Logic supports, or event handlers for analytics. For anything that requires a JavaScript library to be loaded first, add the library link in the Custom Header or Custom Footer field and put your script code here.

‍

Add custom JavaScript

  1. In the Receive module, go to Form management and open the relevant form template.
  2. Click the Custom HTML/Javascript tab.
  3. In the Custom Javascript field, enter your JavaScript. Write only JavaScript here — no <script> tags needed.
  4. Click Update to save your changes.
  5. Click Publish to make the updated template live for new form instances.
‍No <script> tags needed: CX Platform wraps your code in a <script> tag automatically. If you include <script> tags in the field, the code may not run as expected. Enter JavaScript only.

‍

What to use it for

The Custom Javascript field is well-suited for:

  • Client-side validators — add custom validation logic that runs before the form is submitted, such as checking that two fields match or that an entered value meets a specific format.
  • Minor UI behaviour — show or hide elements, update field labels, or respond to user interactions in ways that go beyond what's available in the Form Logic tab.
  • Analytics event handlers — fire custom events to your analytics platform when users interact with the form, such as tracking field completions or page progression.
‍Need a JavaScript library? If your script depends on an external library (like a custom validation plugin or analytics SDK), add a <script src="..."> link in the Custom Footer field to load the library, then write your code that uses it in the Custom Javascript field. Third-party libraries should be hosted within the CX Send module rather than linked externally where possible — check with your implementation team if you're unsure. For detailed guidance on the API and JavaScript integration options, refer to the API documentation.

‍

Once saved and published, your script will run on every new instance of the form after the page has loaded. Use Test Form to verify your JavaScript is behaving as expected before publishing.

‍