Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Functions

What is the use of writing a jQuery function like so...

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

What i mean is why wrap the function in $

like image 868
Dooie Avatar asked Feb 21 '11 10:02

Dooie


People also ask

How many types of functions are there in jQuery?

5 Different Ways to Declare Functions in jQuery.

Can we use function in jQuery?

In jQuery we can assign function in custom namespace. We can add or remove the function from namespace. This mechanism provide the functionality of storing the related function in a namespace.

What is $() in jQuery?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)

How do you create a function in jQuery?

Answer: Use the syntax $. fn. myFunction=function(){} The syntax for defining a function in jQuery is little bit different from the JavaScript.


2 Answers

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.

like image 82
Guffa Avatar answered Oct 13 '22 00:10

Guffa


It's a shortcut for

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

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

like image 42
Flo Avatar answered Oct 13 '22 00:10

Flo