jQuery: Prevent collision of $

Other language site
ja ja
Google Translate
  • -

    シェア
  • ---

    LINEで送る
  • -

    はてなブックマーク
    ブックマーク
  • -

    pocket
  • -

    rss
jQuery-Logo

jQuery uses $. However, sometimes $ is not recognized as jQuery and it may be an error. That's the solution.

Cause $ collision definition

jQuery uses $. However, this $ may not be recognized as jQuery and a script error may occur.
This happens because there is a JavaScript plugin that uses $ for non jQuery, and a $ definition conflict occurs.

To avoid this, you can solve it by enclosing the code that uses jQuery as follows.

(function($) {
  // Write code using jQuery here.
})(jQuery);

This means that $ used in function ($) {} has the same meaning as jQuery. In other words, $ explicitly defines jQuery.

When using jQuery you may decide to use (function ($) {}) (jQuery).

Supplement

You can solve it by rewriting "$" part of jQuery as "jQuery".
However, it is not recommended because this method requires writing "jQuery" every time.

SNS also distributes articles.
Leave a Reply

*

If you like this article, share it!