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") 
5
  • 2
    its not the same question Commented Jan 6, 2013 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) Commented Jan 6, 2013 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. Commented Jan 6, 2013 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. Commented Jan 7, 2013 at 11:42
  • @user1721135 stackoverflow.com/questions/1049112/… is not the exact same question, but it answers this one. Commented Jan 7, 2013 at 11:44

7 Answers 7

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

Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

Except that that code doesn't set up this alias.
@LightnessRacesinOrbit: How'd you mean?
@Matt: It's just been edited. A minute ago it was completely different code pasted into this answer: code that had nothing to do with an alias between $ and jQuery.
@LightnessRacesinOrbit True. I had for a minute the wrong code pasted.
@Matt: Logic dictates that this remains a possibility. :)
|
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'); 

1 Comment

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.
4

Yes, they are the same, see here:

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

1 Comment

+1 best resource short of reading source code is the official API
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.

Comments

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.

4 Comments

statement isn't quite accurate...jQUery is the main object, $ is the alias which can conflict
@charlietfl thanks, got confused when editing
conflicts are usually handled with noConflict() so concept is still not on track
I agree. never said anything different.
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()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.