Reputation: 1669
What is the use of writing a jQuery function like so...
$(function myFunction() {
...
});
What i mean is why wrap the function in $
Upvotes: 8
Views: 6664
Reputation: 19
It actually happens to be a short-hand for the following syntax:
function handleDocumentReady ()
{ // handleDocumentReady ()
// Code to handle initialization goes here...
} // handleDocumentReady ()
$(document).ready (handleDocumentReady);
Upvotes: 1
Reputation: 2778
It's a shortcut for
$(document).ready(function myFunction() {
...
});
See http://api.jquery.com/ready/
Upvotes: 3
Reputation: 700242
I think that you mean like this:
$(function() {
...
});
This is shorthand for:
$(document).ready(function() {
...
});
What it does is registering a handler for the ready
event, so the code in the function will be run as soon as the document has loaded.
Upvotes: 12