Reputation:
I am reading JavaScript and JQuery, The Missing Manual
and they start of with this Snippet :
$(document).ready(function(){});
I know that function(){}
is an anonymous function, and that document is an object with properties I can set / read, and that ready() is a JQuery function defined in the library, but I don't know what the rest of the syntax is for and it is not explained in the book.
Particularly,
$(document)
Can someone explain what this does or point me to a link? Also, someone said that you can identify JQuery by this alone, is this true?
Upvotes: 7
Views: 3940
Reputation: 46657
$
is an alias (short-hand) for the variable jQuery
which is the blanket object that stores all jQuery functions.
$(document)
is taking your current window.document
(the window.
part is often omitted when accessing window properties) and passing it to the jQuery constructor $()
, and then attaching an event handler to the ready
event, which executes the anonymous function passed as a callback.
Upvotes: 1
Reputation: 11683
The $ is a synonym for jQuery and what that does is described here: http://api.jquery.com/jQuery/
Upvotes: 1
Reputation: 2407
the $ before jquery statements is to differentiate between standard javascript and jquery. But other frameworks can use the dollar sign as well, so sometimes you will see jQuery(document) so as not to conflict. It can also be set to anything really, even $jq, etc. All it is doing is telling your code to use the framework functions instead of standard javascript.
Upvotes: 1
Reputation: 171698
$
is a shortcut for the JQuery object. All methods in the jQuery library are part of the jQuery object.
$(selector)
is the same as writing 'jQuery(selector)`
Upvotes: 1
Reputation: 725
$ is just a selector for jquery. You're pretty much saying that what follows after "$" is part of the jquery library.
Be careful because some other javascript libraries use that same selector.
Upvotes: -3
Reputation: 1075925
$(document)
wraps a jQuery instance around the document
object. ($
is just an alias for jQuery
.) So the return value of $(document)
is a jQuery instance, which has a ready
function on it.
Upvotes: 4