Dooie
Dooie

Reputation: 1669

jQuery Functions

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

Answers (3)

J. T. Marsh
J. T. Marsh

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

Flo
Flo

Reputation: 2778

It's a shortcut for

$(document).ready(function myFunction() {
    ...
});

See http://api.jquery.com/ready/

Upvotes: 3

Guffa
Guffa

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

Related Questions