When, if ever, should you use `jQuery` over `$`?
When, if ever, should you use `jQuery` over `$`?
I was looking through some code and noticed:
button: function(e) {
e.preventDefault();
var $target = jQuery(e.target);
var link = $target.attr('href');
I was just a little unclear about the line var $target = jQuery(e.target);.
var $target = jQuery(e.target);
Why use jQuery here?
jQuery
$target
Possible duplicate of What is jQuery(document) vs. $(document)
– Esko
Jul 2 at 6:18
If you are looking for $ vs jQuery..
$ is an alias/shorthand of jQuery period– Saurabh Sharma
Jul 2 at 6:20
$
jQuery
@SaurabhSharma . . . except if
jQuery.noConflict() has been called, or if another library on the page has reassigned $.– AKX
Jul 2 at 6:21
jQuery.noConflict()
$
Maybe there is another framework that also uses the $ ?
– Goot
Jul 2 at 6:21
1 Answer
1
Usually you would like to use jQuery instead of $, when the latter conflicts with a globar variable from another library.
jQuery
$
For instance, see this list: What JavaScript libraries are known to use the global dollar sign: window.$?
In that case, jQuery provides the noConflict() method, which:
noConflict()
Relinquish jQuery's control of the $ variable.
The documentation also states:
In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $.noConflict(). Old references of $ are saved during jQuery initialization; noConflict() simply restores them.
$
jQuery
$
$
$.noConflict()
$
noConflict()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
$targetis just a variable name...it can be anything– Aravind S
Jul 2 at 6:18