JQuery is not defined in Wordpress, but my script is queued correctly

I am trying to load a separate javascript mobile-menu.js file into a Wordpress theme. When I look at the console, it says: "jQuery is not defined." However, I know that I have implemented my script files correctly. Any ideas?

HTML file:

<a href="#" id="menu-icon"></a> <!--this line wasn't here originally--> <div id="switchmenu"><!--switchmenu begin--> <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?> </div><!--switchmenu end--> 

functions.php file:

 function lapetitefrog_scripts() { wp_enqueue_style( 'lapetitefrog-style', get_stylesheet_uri() ); wp_enqueue_script( 'lapetitefrog-mobile-menu', get_template_directory_uri() . '/js/mobile-menu.js', array(), '1.0', true ); } add_action( 'wp_enqueue_scripts', 'lapetitefrog_scripts' ); 

mobile-menu.js file:

  jQuery(document).ready(function($) { $('#menu-icon').click(function() { $('#switchmenu').slideToggle("fast"); }); }); 
+6
source share
4 answers

Add wp_enqueue_script('jquery'); before you embed your scripts.

+12
source

First make sure the jquery file is included in the header and your file is jQuery

 wp_enqueue_script( 'lapetitefrog-mobile-menu', get_template_directory_uri() . '/js/mobile-menu.js', array('jquery'), '1.0', true ); 

Secondly, you should run your javascript file, for example:

 (function($) { $(document).ready(function() { ....... }); })(jQuery); 

OR

 // Use jQuery in place of $ jQuery(document).ready(function() { ..... }); 
+4
source

You can try:

first enter jquery.

 wp_enqueue_script('jquery'); 

and then enqueuing the last script with a jQuery dependency in the third argument ie array('jquery') , which is mostly forgotten by people.

 wp_enqueue_script( 'lapetitefrog-mobile-menu', get_template_directory_uri() . '/js/mobile-menu.js', array('jquery'), '1.0', true ); 
+2
source

Wordpress comes with jQuery by default

 // Adds jQuery add_action('init', 'childoftwentyeleven_down'); function childoftwentyeleven_down() { // register your script location, dependencies and version wp_register_script('down', get_stylesheet_directory_uri() . '/js/down.js', array('jquery') ); // enqueue the script wp_enqueue_script('down'); } 
0
source

Source: https://habr.com/ru/post/981798/


All Articles