plugins - jQuery works instead of $

admin2025-04-19  0

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

Share Improve this question asked Oct 25, 2019 at 13:19 Atta Ur Rehman AkbarAtta Ur Rehman Akbar 214 bronze badges 1
  • 1 Generall jQuery works better than $ even when $ works – Tom J Nowell Commented Oct 25, 2019 at 13:42
Add a comment  | 

2 Answers 2

Reset to default 1

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

  1. use jQuery instead of $
  2. encapsule your calls like RiddleMeThis proposed

On a personal note: honestly, it's not that big a deal to replace the $ with jQuery, isn't it? ;)

Happy Coding!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745053773a282277.html

最新回复(0)