Custom jQuery not working on Wordpress Ninja Forms plugin

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

Answers


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...
});

The event isn't registered simply because the elements you're trying to bind the event to do not exist yet at that moment (on document load). Ninja forms loads the form contents asynchronously, so you'll have to wait until the form is fully loaded and then add your event listeners. This works for me:

var formExists = setInterval(function() {
  if ($(".nf-form-cont").length) {
    // Set your event listeners here, example:
    $("#nf-field-1").click(function(e) {
      console.log("click!");
    }
    clearInterval(formExists);
  }
}, 100); // check every 100ms

Need Your Help

Indexing one array by another in numpy

python numpy

Suppose I have a matrix A with some arbitrary values:

Java / Swing : JTextArea in a JScrollPane, how to prevent auto-scroll?

java swing jscrollpane jtextarea

here's a runnable piece of code that shows what my "problem" is.