Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery is not defined in Wordpress, but my script is enqueued properly

Tags:

I am trying to load a separate javascript file mobile-menu.js to my Wordpress theme. When I look at the console, it says, "jQuery is not defined." However, I know that I enqueued 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");     }); }); 
like image 753
nicatoby Avatar asked Jan 31 '15 04:01

nicatoby


People also ask

Does WordPress load jQuery by default?

WordPress automatically loads it along with the user script. Here is an example code. This code adds a custom javascript and calls jQuery as its dependency. wp_enqueue_script( 'custom-script' , get_stylesheet_directory_uri() .


1 Answers

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

like image 155
Gavin Simpson Avatar answered Oct 24 '22 09:10

Gavin Simpson