Gform_post_render not working with non ajax form and external file [RESOLVED]

I’m using the gform_post_render javascript hook to add a class to input fields if they have a value. Basic sample:
$(document).on(‘gform_post_render’, function()

I have this code in a separate JS file. If the form is ajax, it works. If the form is not ajax, it does not work. However if I put the same code in a script tag underneath the short code that prints the form (in the template php file) it works.

Due to that I now have that code repeated a few times. Once in a JS file for ajax forms, and a couple more in template files for my non ajax forms. Is there a way to make all forms us just the code in the JS file?

How are you including the script when you have it as an external file?

With wp_register_script(), loaded in the footer.

Rather than using wp_register_script(), have you tried using gform_pre_render as explained in the doc page?

Placement

This code can be placed in an HTML field on your form or you may use the gform_pre_render hook to echo the script block to the page.

Feels odd to print JS code with PHP, but it works, and I no longer have duplicate code. Thanks!

1 Like