3

Normally when I am writing my jquery code I do something like

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

I was looking at some code online and I noticed that the author did this

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

What is the use of the $ as the function parameter

tawheed
  • 5,565
  • 9
  • 36
  • 63
  • Look at the documentation: http://api.jquery.com/ready/ - it's pretty clearly explained in the `Aliasing the jQuery Namespace` section – Ian Jun 26 '13 at 13:51
  • This explains it all - http://www.bennadel.com/blog/1719-jQuery-s-Passes-Itself-As-An-Argument-To-The-Ready-Event-Callback.htm – techfoobar Jun 26 '13 at 13:52
  • 1
    http://stackoverflow.com/questions/4983150/jquery-dollar-sign-as-function-argument – Sergio Jun 26 '13 at 13:52

3 Answers3

7

jQuery calls the callback function with jQuery as the first argument. Javascript doesn't require you to define parameters that will be passed to your function so it's usually left out if it's not needed.

Here it seems weird because the author is already relying on $ being jQuery - you would normally expect it to be along the lines of:

jQuery(document).ready(function($) {
    // $ works here even if someone changed the global `$`
    // this breaks down if someone changed jQuery too but that's far less likely
});
Esailija
  • 138,174
  • 23
  • 272
  • 326
0

The jQuery function is the value of jQuery or of $. It serves as a namespace so we can call it "The global jQuery object."

Alex Marinov
  • 191
  • 1
  • 5
0

Buddy Please jquery documentation.Its well written and easy to understand . Any way i will tell you what is the $ sign. $ is a shortcut to the jQuery function. .

 **$**(document).ready(function() {

  // statements

 });

Here $ represents jquery .You can use jquery instead of $ sign..

See this link Click here

Jijo John
  • 1,375
  • 9
  • 21