I am using $ in my custom js file in a wordpress child theme. But it says $ is not defined. But jQuery works fine. What is the reason? Is it because $ is used in newer version of jQUery and WP uses old version 1.12.4. Kindly correct me.
Kind Regards
I am using $ in my custom js file in a wordpress child theme. But it says $ is not defined. But jQuery works fine. What is the reason? Is it because $ is used in newer version of jQUery and WP uses old version 1.12.4. Kindly correct me.
Kind Regards
WP uses noConflict(). If you want to use $ instead of jQuery you have a couple options.
Option #1 - use a self-invoking function:
(function($) {
$(document).ready(function(){
...
});
}(jQuery));
Option #2 - Set noConflict():
Add var $ = jQuery.noConflict();
at the top of your custom JS file.
As different JS Frameworks use the $ for it's functions (jQuery, MooTools), Wordpress loads jQuery in the "noConflict"-Mode, which means that it doesn't use the $ but only jQuery.
You can now
On a personal note: honestly, it's not that big a deal to replace the $ with jQuery, isn't it? ;)
Happy Coding!
jQuery
works better than$
even when$
works – Tom J Nowell ♦ Commented Oct 25, 2019 at 13:42