0

Adding scripts for my theme and added jquery as a dependency. I see the jquery script tag in the header of the page but my script in the footer is getting a "$ is not a function" error.

function wpdocs_template_scripts() {
    wp_enqueue_style( 'style', get_stylesheet_uri() );
    wp_enqueue_script('modernizer', get_template_directory_uri() .'/js/vendor/modernizr-2.8.3.min.js', null, null, false);
    wp_enqueue_script('bootstrap', get_template_directory_uri() .'/js/bootstrap.min.js', array('jquery'), null, true);
    wp_enqueue_script('main', get_template_directory_uri() .'/js/main.js', array('jquery'), null, true);
}
add_action( 'wp_enqueue_scripts', 'wpdocs_template_scripts' );
1

Put this in your .js file

(function($) {
 // Your code inside here;
})(jQuery);

This is called an anonymous function, you're passing the jQuery object as a parameter to it.

OR

   var $ = jQuery; // This at the top of the file

This is simple, you're just assigning the jQuery object to the $ variable

References:

is-not-a-function-jquery-error

jquery-is-not-a-function-error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.