Using jQuery with other Libraries
So you finally got the opportunity to try out jQuery on one of your projects but you are already using a library that commands control of the infamous $
alias or function. Fear not, for no matter the reason the $
may be taken, jQuery is built to be compact and versatile. All of jQuery is contained within the jQuery
namespace and the $
is just an alias for jQuery
. So the following two lines of code will result in the same output.
jQuery('.myClass').hide();
$('.myClass').hide();
With this knowledge creating a different alias is as simple as this line of code.
var $j = jQuery;
However, depending on the order the libraries have been loaded, jQuery might still be in control of the $
and causing existing scripts to fail. The easiest way to solve this would be to just change the order your scripts are included by making sure jQuery is loaded first. This allows the other library to overwrite the $
with its own implementation. Another way that is just as easy, is to use the noConflict
method. The noConflict
method returns the $
back to its original owner and allows you to create a new alias on the fly.
var $j = jQuery.noConflict();
Now you can use $j
and take advantage of all the DOM-fu jQuery provides.