Running jQuery before jQuery is called


#1

Hi there,

Is it possible to run a jQuery line before jQuery is called?

I have the following line which I am ttrying to run:

jQuery('.flex-control-nav').insertBefore('.caption-wrap');

It works in console, but when I add it to a JS file, it doesn’t work.

I am using a WordPress website and using the WordPress enqueue function to insert the JS file, but it appearing before the actual jQuery library.

Is there anyway to run it before the jQuery library line?

Thanks!


#2

Nope. You can’t run that line until the jQuery library is loaded.


#3

You have to specify jQuery as a dependency to ensure the scripts are getting loaded in the correct order:

wp_enqueue_script('myscript', './path/to/myscript.js', ['jquery']);