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?
Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.
The $ sign is nothing but an identifier of jQuery() function. Instead of writing jQuery we simply write $ which is the same as jQuery() function. A $ with a selector specifies that it is a jQuery selector.
In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.
Dollar Sign is just an alias for JQuery. jQuery(document). ready(function(){}); OR $(document).
$(function(){...})
is a shortcut for
$(document).ready(function(){...});
See the API docs
http://api.jquery.com/ready/
$(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With