November 7, 2019: Fixed fatal error when Gravity Forms was not active.
Some of our Perks rely on JavaScript to handle DOM manipulation in the form. For example, Populate Anything uses JS when populating fields and Live Merge Tags. JavaScript hooks, and the snippets we write for those hooks, are always more complicated to integrate into your site. There’s generally two methods you can use to add custom JavaScript to your form, and both of them have their drawbacks.
- The easiest method to include custom JavaScript in your forms is to include them in an HTML Field on the form,
wrapped in
<script>
tags. While an easy method, it can cause some issues with AJAX-enabled forms. No bueno. - The more complex route is to write a custom function to enqueue it and place it somewhere in your theme’s code. Like any custom code, there are a multitude of different methods, and even a seasoned developer can run into issues making sure the script is loaded in the right context and not loaded on pages where it isn’t necessary.
A Better Way
During a discussion about how we recommend managing code snippets to our customers, we came up with a third option: a plugin to easily add custom JavaScript to Gravity Forms. We already recommend Code Snippets to our users for managing their PHP-based snippets, but there wasn’t an easy way to add custom JS to a form and only load it on that particular form. So we made one. It’s called Gravity Forms Custom Javascript, and it adds a Custom Javascript field to your Form Settings.
Getting Started
![]() |
Download, install, and activate the plugin. |
![]() |
Navigate to your Form Settings to find the new Custom Javascript field. |
Once the plugin is installed and activated, you’ll find the Custom Javascript field in your Form Settings. It includes a robust editor that will automatically highlight syntax to make it easy to read.
It even includes code suggestions should you be inclined to write custom JavaScript straight into the editor.
All Snippets Welcome
We didn’t build this solely for Gravity Perks. Gravity Forms has a bunch of available snippets too, and any of the JS ones will work with this plugin. They also maintain a massive library of JavaScript hooks that you can take advantage of. While they aren’t immediately ready for use, most of them have code examples to get you started, and many of those examples are commonly requested features.
Why We Built This
Many of our Perks have additional hooks and filters that you can apply to adjust their behavior outside of the user interface. We typically add these to our products because we have an idea for a new feature, but haven’t worked out the details on how to expose that feature to our customers in a way that is easy to use and clear to understand. Instead of sitting on the feature until we have baked an interface, we publish the hook so it can benefit the customers that need it.
In most cases, simply publishing the hook with some documentation is enough for developers to start using the feature. They’ll write custom code that uses the hook, and their Perks start doing incredible new things. We also often write our own code snippets that use these hooks, and we suggest them to our customers through Support interactions. Our Advanced and Pro Gravity Perks licenses includes Snippet support, and we’re happy to offer a solution that involves dropping in some of our code to extend the capability of a Perk.
Many snippets are blocks of PHP. You copy the code and paste it into your theme’s functions.php file. Depending on the snippet you might need to adjust some parameters like a Form ID or Field ID, and sometimes you need to call a class. If you have trouble with the snippet, we have a guide to walk you through basic troubleshooting.
Questions? Feedback?
Do you have questions about how this works, or is there a feature missing that you would really love to have? We’re always open to feedback and want to know how we can improve this plugin.
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 47+ essential add-ons for Gravity Forms with support you can count on.
Awesome Snippet!
Came across an issue when using placeholders on fields, maybe this can be added by default. Exclude “.gf_placeholder” from the targets eg.
let $targets = $group.not( $elem ).not( ‘.gplc-disabled’ ).not( ‘.gpi-disabled’ ).not( ‘.gf_placeholder’ );
Hi James,
I’m not really following the issue you’re reporting. Where exactly are you getting this code? I’ve emailed you to request for more information on the issue. Please check and reply.
Best,
The snippi.com links posted above are not currently working. Any chance you could post the code somewhere else so we can see it?
Yeah, we’re bummed the snippi fell off. ๐ข
I don’t have that exact code but the spirit of that conversation was around the same concept implemented by another snippet in our library:
https://github.com/gravitywiz/snippet-library/blob/master/gp-limit-choices/gplc-field-groups.php#L216-L217
Can we use this addon for executing a custom JS code when gravity form is successfully submitted ? We want to integrate a LinkedIn event tracker code snippet for tracking form submission.
Hi Pranav,
I haven’t tried any LinkedIn event tracker code to know how it works, but this plugin works best for snippets that are executed when the form renders. If your code will run after the form is submitted, I doubt this would work for you. You may want to try one of these Gravity Forms solutions for adding Javascript snippets to the site.
Best,
When I try adding code to an AJAX form that hooks into gform_confirmation_loaded, it runs twice: https://snippi.com/s/pweh428.
Hey Travis, responding here (rather than the ticket) for the benefit of other folks using this plugin. GF Custom JavaScript executes all JS code via the “gform_post_render” event which fires every time the form is rendered. This is ideal for most circumstances there are exceptions. When binding events on an AJAX-enabled form, there is no page refresh that “clears” out the previous binding so you’ll need to clear it out yourself. Here’s an example:
https://snippi.com/s/o1oumhh
With this in place, you will see that the function is bound when the form is first rendered and again when the confirmation is rendered. It’d be convoluted to try to prevent the second binding altogether but by setting a custom namespace, you can remove the previous binding before it is rebound.
Hope this helps!
Thank you. Here is my original code rewritten using the technique you described: https://snippi.com/s/qmanc5b
Is this the intended implementation? It appears to work and works with GF Reload Form.
I found a different solution in this thread using a global boolean, similar to how Gravity Forms prevents multiple submissions: https://snippi.com/s/tgu0qik
This also appears to work and works with GF Reload Form.
Are there advantages to using the namespace technique over the global boolean?
Nope. That’s perfectly fine. I try to only use variables when necessary for simplicity but its ultimately a personal preference. ๐
I appreciate you guys!
I will use the namespace rebinding technique to avoid globally scoped variables.
For the sake of thoroughness, I believe the global boolean example I gave should be corrected for robustness to avoid conflicts when multiple forms are on the same page: https://snippi.com/s/j3juzef
Thanks again for the quick response.
Epic! Thanks for the update. And always happy to help. ๐