2

Possible Duplicate:
jQuery selecter whats difference between jQuery(“element”) and $(“element”)?

Just a little curious..

Why sometime I see it written as $("#SomeElementId") and sometime as jQuery("#SomeElementId") ? What is the exact difference ?

Community
  • 1
  • 1
skos
  • 4,102
  • 8
  • 36
  • 59

5 Answers5

5

$ is an alias of the jQuery namespace. It's just quicker to type. It is avoided normally when there is the potential for conflict with other libraries that also utilise $.

jQuery always declares jQuery but will only declare $ unless jQuery.noConflict() is called.

Either way, a common pattern is to reference $ as a local variable in a closure, via an immediately-executed function:

(function($) {
    //all your code here, which can reference $
})(jQuery);
Mitya
  • 33,629
  • 9
  • 60
  • 107
3

understanding $ vs. jQuery in iife instead of $

Note same question is asked many time man :) different wording but all meant same thing, so I reckon this question will get closed soon.

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.noConflict(): http://api.jquery.com/jQuery.noConflict/

In "no-confict" mode, the $ shortcut is not available and the longer jQuery is used. For example:

$(document).ready(function(){
     $(#somefunction) ...
});

Becomes:

jQuery(document).ready(function(){
    jQuery(#somefunction) ...
});

In order to use the default jQuery shortcut of $, you can use the following wrapper around your code:

jQuery(document).ready(function($) {
    // $() will work as an alias for jQuery() inside of this function
});

That wrapper will cause your code to be executed when the page finishes loading, and the $ will work for calling jQuery. If, for some reason, you want your code to execute immediately (instead of waiting for the DOM ready event), then you can use this wrapper method instead:

(function($) {
    // $() will work as an alias for jQuery() inside of this function
})(jQuery);

Good read: http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_wrappers

Further if you keen:

What does $ mean in jQuery?

This should help to quench your thirst :) might be hope this helps!

Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
2

They are the same. By default jQuery binds itself to two global names: jQuery and $. The latter is shorter but more ambiguous, as other frameworks do the same.

See the docs about this: http://api.jquery.com/jQuery.noConflict/

Simon Sapin
  • 9,790
  • 3
  • 35
  • 44
2

$ is a shortcut for jquery - lots of javascript libraries allow you to use $, so if you use more than 1 library in your code it is generally safer to use jquery - if you only use jQuery, then you should have no problem using $

ChrisW
  • 4,970
  • 7
  • 55
  • 92
1

There is none, $ is just an alias, you can also use different aliases by using the jQuery.noConflict() function

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
  • You can use different aliases just by making them. `var a = jQuery; var b = jQuery;` You now have 4 ways to reference it. `jQuery.noConflict` is for when you include jQuery after some library that uses `$`, instead of doing it the other way around and letting the other library overwrite it. – Esailija Jul 27 '12 at 11:04
  • You can, although I believe noConflict might do a little more (I may be wrong though). Certainly I've always been advised to use noconflict rather than just assigning a new variable. Ah as per your edit, yeah its generally when I use ther libraries, I guess its just safer either way to use noConflict? – Jon Taylor Jul 27 '12 at 11:05
  • 1
    The release of `$` in `.noConflict` is just an unwanted side effect when you merely want to alias jQuery. It simply does `return jQuery`, you can write that yourself by doing `var a = jQuery` instead of doing `var a = jQuery.noConflict()`. – Esailija Jul 27 '12 at 11:08
  • Fair enough, thanks for the info :) – Jon Taylor Jul 27 '12 at 11:10