Skip to content
Advertisement

WordPress: JS is loaded but functions in it are not working

I have a js file named “main.js” and I enqueued it using wp_enqueue_script in function.php file. This file is getting loaded in my page-{slug}.php, but the functions in it are not executing at all.

The function.php file:

JavaScript

page-{slug}.php file:

JavaScript

main.js file:

JavaScript

In short, I write anything in main.js file, the changes which I expect doesn’t happen or doesn’t reflect in my page-{slug}.php file.

Advertisement

Answer

I think, the problem with jQuery wrong usage. Probably, wrapping the function with jQuery allows using $ variable and could solve the problem

JavaScript

The second required thing is to set jQuery as a dependency for your script and to update the script version to 1.1.

JavaScript
Advertisement