Hire ASP Developer

JQuery Conflict Solution

by | 23 Sep 2013

Review of:
Product by:
Hire ASP Developer

Reviewed by:
On September 23, 2013
Last modified:October 22, 2013


JQuery Conflict Solution

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery’s case, $ is just an alias forjQuery, 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.

The noConflict() method releases the hold on the $ shortcut identifier, so that other scripts can use it.

The solution to resolve the conflict:

Example: You can of course still use jQuery, simply by writing the full name instead of the shortcut:

jQuery("p").text("jQuery is still working!");

Example: You can also create your own shortcut very easily. The noConflict() method returns a reference to jQuery, that you can save in a  variable, for later use. Here is an example:

var jq = $.noConflict();
jq("p").text("jQuery is still working!");

Example: If you have a block of jQuery code which uses the $ shortcut and you do not want to change it all, you can pass the $ sign in as a parameter to the ready method. This allows you to access jQuery using $, inside this function – outside of it, you will have to use “jQuery”:

$("p").text("jQuery is still working!");

Leave a Reply