I have seen a syntax where one puts a function inside parentheses which follow a dollar sign like this:
$(function(){...});
What does this mean in jQuery? What does the function do?
I have seen a syntax where one puts a function inside parentheses which follow a dollar sign like this:
$(function(){...});
What does this mean in jQuery? What does the function do?
$(function(){...})
is a shortcut for
$(document).ready(function(){...});
See the API docs
$(document).ready(handler)
$().ready(handler) (this is not recommended)
$(handler)
The function inside the parentheses is executed when the DOM is fully loaded.
This is implemented by .ready()
, i. e. as Mohammad Adil already said, it's a shortcut.
Excerpt from the documentation for .ready()
:
While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to
.ready()
is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.