0

I've inherited a client website, and I am not a jQuery developer.

The theme header.php contains the following inline JavaScript:

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

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

Do the two occurrences of the code function($) need to be changed to function(jQuery)?

eisbehr
  • 11,374
  • 7
  • 30
  • 56
Steve
  • 2,509
  • 11
  • 48
  • 88
  • `$` is alias for `jQuery`. Is your code working? If yes - no need to change. If no - you need to change code. – Justinas Mar 26 '18 at 06:23
  • 1
    it passed as a parameter(alias) so that you can use `$` inside without any problem instead of writing `jQuery` again and again – Serving Quarantine period Mar 26 '18 at 06:24
  • 1
    Possible duplicates of:- [What does (function($) {})(jQuery); mean?](https://stackoverflow.com/questions/2937227/what-does-function-jquery-mean) – Serving Quarantine period Mar 26 '18 at 06:25
  • `$` is not an alias of `jQuery` in Wordpress, because jQuery runs in no conflict mode there. So what you probably mean is, you make `$` an alias there. ;) @Justinas – eisbehr Mar 26 '18 at 08:05
  • @eisbehr oh, didn't know that in WP – Justinas Mar 26 '18 at 08:12
  • Possible duplicate of [What does (function($) {})(jQuery); mean?](https://stackoverflow.com/questions/2937227/what-does-function-jquery-mean) – Liam Mar 26 '18 at 09:20

1 Answers1

0

No it does not and is not recommended to change that

By default when you enqueue/execute a jQuery script in Wordpress you must use jQuery, and $ is not used, because $ is for compatibility with other 3rd party jQuery libraries.

You better see this article

aswzen
  • 968
  • 14
  • 34
  • 1
    `(function($){ // code here})(jQuery);` wraps jQuery into `$` **in the scope of the closure** here. So there is no harm in using `$` inside of the closure. It does not affect the outer scope as the alias is confined to inside the closure. – Liam Mar 26 '18 at 09:19
  • 1
    [How do JavaScript closures work?](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Liam Mar 26 '18 at 09:21