"Uncaught TypeError: $ is not a function" in WordPress
13,098
There are two solution for this:
#1
Repalce all $
to jQuery
in your code.
#2
Or a Simple way.
just assign jQuery
to $
as a global variable
like following on the top of your .js
file and all your code will work fine.
var $ = jQuery;
I hope this works for you.
Author by
Capitan Duke
Updated on June 27, 2022Comments
-
Capitan Duke almost 2 years
There are some questions about this error, but I couldn't find any solutions on them. I'm developing a WordPress theme and during the enqueue script this error came up:
Uncaught TypeError: $ is not a function at components.js?ver=4.8:65 at components.js?ver=4.8:95
here is my enqueue code add to the functions.php:
function goc_scripts() { wp_enqueue_style( 'goc-style', get_stylesheet_uri() ); wp_enqueue_style( 'socicon', get_stylesheet_directory_uri() . '/css/socicon.css', array() ); wp_enqueue_style( 'bootstrap-social', get_stylesheet_directory_uri() . '/css/bootstrap-social.css', array() ); wp_enqueue_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css'); wp_enqueue_style('font-awesome', get_template_directory_uri() . '/css/font-awesome.min.css'); wp_enqueue_style('simple-line-icons', get_template_directory_uri() . '/css/simple-line-icons.min.css'); wp_enqueue_style('animate', get_template_directory_uri() . '/css/animate.min.css'); wp_enqueue_style('cubeportfolio', get_template_directory_uri() . '/css/cubeportfolio.min.css'); wp_enqueue_style( 'owlcarousel-style', get_template_directory_uri() . '/css/owl.carousel.css' ); wp_enqueue_style( 'fancybox', get_template_directory_uri() . '/css/jquery.fancybox.css' ); wp_enqueue_style( 'goc-slider', get_template_directory_uri() . '/css/slider.css' ); wp_enqueue_style( 'goc-plugins', get_template_directory_uri() . '/css/plugins.css' ); wp_enqueue_style( 'goc-red3', get_template_directory_uri() . '/css/themes/red3.css' ); wp_enqueue_style( 'goc-custom', get_template_directory_uri() . '/css/custom.css' ); wp_enqueue_script( 'goc-jquery-min', get_template_directory_uri() . '/js/jquery.min.js', array(), false, true ); wp_enqueue_script( 'goc-jquery-migrate', get_template_directory_uri() . '/js/jquery-migrate.min.js', array(), false, true ); wp_enqueue_script( 'goc-jquery-easing', get_template_directory_uri() . '/js/jquery.easing.min.js', array(), false, true ); wp_enqueue_script( 'goc-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20151215', true ); wp_enqueue_script( 'goc-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true ); wp_enqueue_script( 'bootstrap-js', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', array(jquery), true); // prueba wp_enqueue_script( 'goc-reveal-animate', get_template_directory_uri() . '/js/reveal-animate.js', array(), '4.8', true ); wp_enqueue_script( 'goc-reveal-animate-wow', get_template_directory_uri() . '/js/reveal-animate/wow.js', array(), false, true ); wp_enqueue_script( 'goc-cubeportfolio', get_template_directory_uri() . '/js/cubeportfolio/js/jquery.cubeportfolio.min.js', array(), false, true ); wp_enqueue_script( 'goc-owl-carousel', get_template_directory_uri() . '/js/owl-carousel/owl.carousel.min.js', array(), false, true ); wp_enqueue_script( 'goc-counterup', get_template_directory_uri() . '/js/counterup/jquery.counterup.min.js', array(), false, true ); wp_enqueue_script( 'goc-waypoints', get_template_directory_uri() . '/js/counterup/jquery.waypoints.min.js', array(), false, true ); wp_enqueue_script( 'goc-fancybox', get_template_directory_uri() . '/js/fancybox/jquery.fancybox.pack.js', array(), false, true ); wp_enqueue_script( 'goc-jquery.smooth-scroll', get_template_directory_uri() . '/js/smooth-scroll/jquery.smooth-scroll.js', array(), false, true ); wp_enqueue_script( 'goc-slider', get_template_directory_uri() . '/js/slider-for-bootstrap/js/bootstrap-slider.js', array(jquery), false, true ); wp_enqueue_script( 'goc-component', get_template_directory_uri() . '/js/components.js', array(jquery), '4.8', false ); wp_enqueue_script( 'goc-components', get_template_directory_uri() . '/js/components-shop.js', array(), '4.8', true ); wp_enqueue_script( 'goc-appjs', get_template_directory_uri() . '/js/app.js', array(), false, true ); if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { wp_enqueue_script( 'comment-reply' ); } } add_action( 'wp_enqueue_scripts', 'goc_scripts' );
and here is the code components.js where the error happens (line 65 and line 95):
var LayoutHeader = function () { var offset = parseInt($('.c-layout-header').attr('data-minimize-offset') > 0 ? parseInt($('.c-layout-header').attr('data-minimize-offset')) : 0); var _handleHeaderOnScroll = function () { if ($(window).scrollTop() > offset) { $("body").addClass("c-page-on-scroll"); } else { $("body").removeClass("c-page-on-scroll"); } } var _handleTopbarCollapse = function () { $('.c-layout-header .c-topbar-toggler').on('click', function (e) { $('.c-layout-header-topbar-collapse').toggleClass("c-topbar-expanded"); }); } return { //main function to initiate the module init: function () { if ($('body').hasClass('c-layout-header-fixed-non-minimized')) { return; } _handleHeaderOnScroll(); _handleTopbarCollapse(); $(window).scroll(function () { _handleHeaderOnScroll(); }); } }; }();
-
Capitan Duke almost 7 yearsHello @Jithin Raj P R - I replace the $ to JQuery in my components.js file (both ways) but nothing happens, the same error is keep on going, is driving me crazy. The same error : Uncaught TypeError: $ is not a function at components.js?ver=4.8:65 at components.js?ver=4.8:95
-
Jithin Raj P R almost 7 years@CapitanDuke can you share me a live link of your file so i can check.
-
Ataur Rahman Munna almost 7 yearsYou should clear your browser cache also and check that again. @CapitanDuke
-
Capitan Duke almost 7 years@Jithin Raj P R Thanks !!! Yes probably was something with the cache that I kept seeing the bug ! :)