Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of creating jQuery code inside this elements like this

    (function ($) {
       ...
    } ) (jQuery);
like image 810
kaneda Avatar asked Jul 20 '10 20:07

kaneda


2 Answers

To avoid conflicts with other javascript libraries that also use $.

This method, however, allows you to use $ in that function at your will, no need to use jQuery there.

That pattern is also important when writing jquery plugins.

like image 71
Sarfraz Avatar answered Sep 23 '22 14:09

Sarfraz


It creates a function, with $ as an argument, and immediately runs that function with jQuery as the argument. Effectively this will ensure that $ points to jQuery inside your code, even if jQuery.noConflict() is used.

like image 37
gnarf Avatar answered Sep 24 '22 14:09

gnarf