0

Possible Duplicate:
What is the meaning of symbol $ in jQuery?
$ versus jQuery

I am a bit new to jQuery but have done some things with it. however i have never used jQuery() function and am curious what the purpose of it is.

Are these the same?

jQuery("body") 

$("body")
Community
  • 1
  • 1
user1721135
  • 6,864
  • 8
  • 34
  • 63
  • 2
    its not the same question – user1721135 Jan 06 '13 at 14:52
  • 3
    I'm not too sure why this has been closed, let alone almost deleted. Has anyone even looked at the second part of the question? (which is perfectly valid on Stack Overflow, AFAIK) – Matt Jan 06 '13 at 15:37
  • @Matt the second part of the question wasn't here when I answered. And it only makes two questions instead of one. *Now* I have reasons to vote to delete. – Denys Séguret Jan 06 '13 at 16:14
  • 1
    I edited out the second question; there's nothing wrong with the first question (unless it's a duplicate), but having two separate questions in the same post is not ok. – JJJ Jan 07 '13 at 11:42
  • @user1721135 http://stackoverflow.com/questions/1049112/what-is-the-meaning-of-symbol-in-jquery is not the exact same question, but it answers this one. – JJJ Jan 07 '13 at 11:44

7 Answers7

9

$ is an alias for jQuery. You can use one or the other.

You can deactivate the $ with jQuery.noConflict(); in case of a conflict

slfan
  • 8,950
  • 115
  • 65
  • 78
9

From the source code :

// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

So yes, it's the same. But you have two accesses, so that you avoid conflicts.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
5

To be the only answer to address the second part of your answer:

jQuery.data() requires an element as the first parameter, which dictates which element you wish to retrieve or set information for.

.data() operates on a jQuery object, and internally calls jQuery.data(), passing the element(s) contained within the jQuery object as the first parameter [source].


TLDR:

jQuery.data(document.body, 'foo', 'bar') === $(document.body).data('foo', 'bar');
Matt
  • 74,352
  • 26
  • 153
  • 180
  • 2
    You can better say that functions prefixed with `jQuery` in the API documentaion means that you access them as a static function (like `$.data()`/`jQuery.data()` or `$.ajax()`/`jQuery.ajax()`), otherwise it is a instance function, you first need to construct a new jQuery instance (with `$(...)` or `jQuery(...)`) in order to use the functions. – Wouter J Jan 06 '13 at 16:00
4

Yes, they are the same, see here:

In the first formulation listed above, jQuery() — which can also be written as $() (...)

Joril
  • 19,961
  • 13
  • 71
  • 88
4

Here is a good explanation from jQuery forum.

$ and jQuery both point to the window.jQuery object, so they are one and the same. the reason some scripts use jQuery instead of $ is to prevent conflicts with other libraries such as prototype or different versions of jquery which both also use the $ variable.

halilb
  • 4,055
  • 1
  • 23
  • 31
0

Basicly, it's an alias. Usually it's used to avoid conflict with other libraries (Prototype for example) that uses the same $. $ is just a name - names in JavaScript can contain dollar signs, and can consist of just a dollar sign.

check out this answer or this one.

Community
  • 1
  • 1
Kuf
  • 17,318
  • 6
  • 67
  • 91
0

Complementing the answers, the .data and jQuery.data() functions are complementary, because one of the (.data) is aplyed in jQuery objects, i.e., the result of a selection operation. Example:

$( "#someId" ).data( "foo" );

And the another is more general method. Both of them are used to associate arbitrary data to DOM elements (and read the HTML5 data atribute). Take a look at the docs descriptions:

jQuery.data()

.data()

davidbuzatto
  • 9,207
  • 1
  • 43
  • 50