Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between $ and $() in jQuery

Tags:

jquery

Could someone take the effort to explain me the difference between $ and $() in jquery?

I know $() is shorthand form of $jQuery() which takes any DOM element and turns it into a jQuery object.

But what I am not sure of is what is $ and how different is it from $(). Kindly enlighten me.

Thanks heaps, Chaitanya

like image 380
Chaitanya MSV Avatar asked Jul 03 '12 11:07

Chaitanya MSV


2 Answers

$ is a function that can be called - $().

The behaviour of $() varies immensely depending on the parameters supplied, although all examples below will return a jQuery object. It can:

  1. register a document.ready handler - $(myfunc)
  2. act as a selector - $('#myid')
  3. construct elements - $('<div>')
  4. return an empty object - $()

$ is also an object that contains various utility functions $.each, etc as properties of that object. In this context, it acts like a namespace for those functions.

like image 105
Alnitak Avatar answered Oct 13 '22 10:10

Alnitak


$ = jQuery - This is the jQuery object which is used for the jQuery Utilies it contains such as $.Ajax() or $.each() Thus var j = $; will assign the jquery prototype into the variable j.

$() = jQuery() - This is a function called from the root jQuery object that is used convert DOM elements to jQuery Objects or get jQuery objects using selectors

like image 35
secretformula Avatar answered Oct 13 '22 11:10

secretformula