Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this JQuery code mean and when will it get called?

I am new to JQuery and I see a piece of code in a web page:

$(function() {
     //do something...
});

I don't understand the syntax here. Does it define a function? When will it get called? Can I re-use it?

Thanks

like image 692
user518138 Avatar asked Jan 26 '26 07:01

user518138


1 Answers

That is the short version of jQuerys DOM ready handler. The code inside the function will be called on page load, when the DOMReady event has fired.

It is equivalent to the following which is a little more verbose, but clearer:

$(document).ready(function() {
    // code here
});
like image 169
Rory McCrossan Avatar answered Jan 28 '26 21:01

Rory McCrossan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!