javascriptjquery

What does it do to invoke jQuery with a function expression argument, like `jQuery(function() { … })`?


jQuery(function ($) { /* … */ });

I am trying to learn jQuery and came across some code that started like the above statement. What does that mean?


Solution

  • That is a shortcut for:

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

    It sets up an event handler for when the document is ready to have its DOM manipulated. It's good practice to initiate your jQuery code after the document is ready. The first parameter $ references the jQuery object so you can use $ in place of jQuery in your code.