Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery syntax: function inside parentheses after dollar sign [duplicate]

Tags:

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?

like image 568
william007 Avatar asked Apr 26 '13 17:04

william007


People also ask

What does '$' mean in JavaScript?

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.

What does dollar sign ($) mean in jQuery?

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.

What does $() mean in jQuery?

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.

What does the dollar sign mean in Ajax?

Dollar Sign is just an alias for JQuery. jQuery(document). ready(function(){}); OR $(document).


2 Answers

$(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)
like image 150
Mohammad Adil Avatar answered Oct 12 '22 01:10

Mohammad Adil


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.

like image 37
nalply Avatar answered Oct 12 '22 03:10

nalply