Custom jQuery not working on Wordpress Ninja Forms plugin

Philipos D. picture Philipos D. · Nov 29, 2016 · Viewed 10.8k times · Source

I am having some problems when I want to add custom jQuery code that affects the form.

For example when someone clicks an input or radio button another input or element to be hidden or shown.I tried to get a result like console.log('trigger'); when clicked or something else but nothing in dev. console appeared.Also, I tried the following methods:

To call the click event with .on('click', function()... or to call the event with .trigger('click');, or to change the event to change

To embed the script within a file from ninja forms or to put it inside the page at the ending of body tag in footer.php

To change the opening declaration of jQuery to work inside a function like this : (function($) {$(document).ready(function(){.....

I know that I could try another plugin, I tried one and the custom jQuery works but I really like this one and don't know why this is happening ...

Thanks

Answer

Mat picture Mat · Aug 2, 2017

Not sure if you need help with this any more as it's been some time since you posted your question, but this may help others in the future. I had the same/similar issue with not being able to run JS/jQuery on the Ninja Forms and found that it's because Ninja Forms load their forms asynchronously. So, when your document.ready function runs, the form doesn't yet exist and it's not able to bind.

Ninja Forms have their own event ready state that can be used as follows:

jQuery(document).on( 'nfFormReady', function( e, layoutView ) {
    // Your code goes here...
});