How to fix TypeError: $ is not a function in WordPress

H

We can guess. You purchased a theme or plugin and a console shown a message “TypeError: $ is not a function”? Ok, maybe you did not purchase anything and the TypeError was created with no reason or with manual customization. It doesn’t matter. What matters, how to fix the problem!

How can you fix that:

The first solution is to rename all references of $ to jQuery:

$( "a" ).hide(); to jQuery( "a" ).hide();

The second solution is to wrap your code using:

<script> 
jQuery(function($) {
    ...your code goes here
});
</script>

Or you can simply add to the top of the problematic .js document (or to the tag):

<script>
var $ = jQuery.noConflict();
</script>

Using of jQuery.noConflict() function causes that $ is undefined and you are probably writing in a .js document $ in a front like this:

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

And you should be writing this:

jQuery(document).ready(function($) {
    // your code goes here 
});

WordPress may use $ for something other than jQuery and so you need to load jQuery in a different way. 

I hope that your problem was fixed with theses solutions. If your problem persists, leave a comment and we’ll try to help you.

Add comment

Recent Posts

Categories

Recent Comments

Archives