jQuery and Other Libraries Conflict


Summary:

  • Use jQuery.noConflict() to make jQuery play nice with other JS libraries.

 
I’ve been working on a project that runs a script within a site. I don’t have any control over that site. That site uses Prototype. When I first tried to run a jQuery function in the script, I got an error. From the error, I could tell that there was a conflict between jQuery and Prototype.

After a bit of searching, I found out how to use jQuery and other libraries safely in one page. It’s built right into jQuery: http://docs.jquery.com/Using_jQuery_with_Other_Libraries.

After loading jQuery, call jQuery.noConflict(). To use jQuery after that, you spell out the name:

var element1 = jQuery('#el');

If you assign the above function to a variable, you can use that variable name as the reference to jQuery:

var jq = jQuery.noConflict();

var element1 = jq('#el');


Leave a Reply

Your email address will not be published. Required fields are marked *