gform/post_render

Description

The gform/post_render event (also known as gform/postRender) can be used to initialize scripts when a form loads. It is a direct replacement for gform_post_render.

Usage

The gform/post_render event can be used with the standard addEventListener method:

document.addEventListener('gform/post_render', (event) => {
	// Do something.
});
document.addEventListener('gform/postRender', (event) => {
	// Do something.
});

Parameters

  • event JavaScript Object
    The event object.
    • detail JavaScript Object
      The event detail object.
      • formId Number
        The ID of the form that was rendered.
      • currentPage Number
        1 for a form that does not use page fields, or the current page number of a multipage form.

Examples

Accessing the event details

document.addEventListener('gform/post_render', (event) => {
	const formId = event.detail.formId;
	const pageNum = event.detail.currentPage;
});

Aborting submission on Enter

The following example shows how you can prevent a specific form, in this case ID 2, from submitting if the user presses the enter/return key when adding a value to an input.

document.addEventListener('gform/post_render', (event) => {
	if (event.detail.formId !== 2) {
		return;
	}

	const isApplicableEvent = (keypressEvent) => {
		const target = keypressEvent.target;

		// Check if the key pressed is Enter and the target is not a textarea, submit button, or button
		return keypressEvent.key === 'Enter' && !(target.tagName === 'TEXTAREA' ||
			(target.tagName === 'INPUT' && (target.type === 'submit' || target.type === 'button')));
	};

	document.addEventListener('keypress', (keypressEvent) => {
		if (isApplicableEvent(keypressEvent)) {
			keypressEvent.preventDefault();
		}
	});
});

Placement

Your code snippet can be placed in an HTML field on your form or in a theme custom JavaScript file.

See also the JavaScript/jQuery section in this article: Where Do I Put This Code?

Since

This event was added in Gravity Forms v2.9.0

Source Code

This event is located in GFFormDisplay::post_render_script() in /form_display.php.