jQuery API

jQuery.noConflict()

jQuery.noConflict( [removeAll] ) Returns: Object

Description: Relinquish jQuery's control of the $ variable.

  • version added: 1.0jQuery.noConflict( [removeAll] )

    removeAllA Boolean indicating whether to remove all jQuery variables from the global scope (including jQuery itself).

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():

<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  $.noConflict();
  // Code that uses other library's $ can follow here.
</script>

This technique is especially effective in conjunction with the .ready() method's ability to alias the jQuery object, as within callback passed to .ready() we can use $ if we wish without fear of conflicts later:

<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  $.noConflict();
  jQuery(document).ready(function($) {
    // Code that uses jQuery's $ can follow here.
  });
  // Code that uses other library's $ can follow here.
</script>

If necessary, we can free up the jQuery name as well by passing true as an argument to the method. This is rarely necessary, and if we must do this (for example, if we need to use multiple versions of the jQuery library on the same page), we need to consider that most plug-ins rely on the presence of the jQuery variable and may not operate correctly in this situation.

Examples:

Example: Maps the original object that was referenced by $ back to $.

jQuery.noConflict();
// Do something with jQuery
jQuery("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';

Example: Reverts the $ alias and then creates and executes a function to provide the $ as a jQuery alias inside the functions scope. Inside the function the original $ object is not available. This works well for most plugins that don't rely on any other library.

jQuery.noConflict();
(function($) { 
  $(function() {
    // more code using $ as alias to jQuery
  });
})(jQuery);
// other code using $ as an alias to the other library

Example: You can chain the jQuery.noConflict() with the shorthand ready for a compact code.

jQuery.noConflict()(function(){
    // code using jQuery
}); 
// other code using $ as an alias to the other library

Example: Creates a different alias instead of jQuery to use in the rest of the script.

var j = jQuery.noConflict();
// Do something with jQuery
j("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';

Example: Completely move jQuery to a new namespace in another object.

var dom = {};
dom.query = jQuery.noConflict(true);

Result:

// Do something with the new jQuery
dom.query("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';
// Do something with another version of jQuery
jQuery("div > p").hide();

Support and Contributions

Need help with jQuery.noConflict() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to jQuery.noConflict()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • http://twitter.com/pg_kansas PG

    http://ralphwhitbeck.com/2009/01/24/jQueryVsPro…

    When Prototyping is used – great use of $().

  • Hariprasad Mails

    may no one know this its really grt

  • Abc

    j hale ni hune ho comment.

  • http://www.tweetaboogle.com/ Tweetaboogle

    Looking at the code the deep noConflict will return the old jQuery object not the new one that is just loaded, true ?

  • Rajkumar967452

    please tell me where i can use this code in which file

  • Lk

    xy

  • bad_cluster

    Situation:
    I want to use my script for greasemonkey with one page.
    I need to use .delegate() in this script.
    The page already has <script>…jquery1.2.6</script>.
    What code should i use to replace library and use a new one?

  • Ssss

    sadasdasdsad

  • Adesh

    Anyone got ideas on how to solve conflicts between two jquery libraries loaded on the same page

    • Avinash

      This looks interesing,

      I solved it by using true parameter in .noConflict(true)
      but it still gives problems with jquery ui and is not perfect

    • Olgish

      I want to know how to do that also!

  • divyajoseph

    hi

  • jon

    can u explain this in a simple method ?

  • jon

    can u explain this in a simple method ?

  • Joopy

    Every time I use Jquery.noconflict it just disables all jquery on the page. Am I missing something?

  • Test

    jjjjjjjj

  • lily

    I need help, please! This isn't working for me. I'm using 3 different jquery scripts, and I've tried a bunch of things, but so far there's always at least one that just doesn't work.

    I'm using a script for jquery tabs, videolightbox, and fancybox. And all have to be on the same page. I don't really speak javascript or php, so i'm kinda a n00b when it comes to this stuff. I'd appreciate any help! Thanks!!

    • http://twitter.com/stARTersart stARTers

      You don't need this method at all.
      This is for using more versions of the whole jQuery framework, not just of its plugins.
      You can just include one script for the jQuery library, and after that, the three scripts you need.

      • Sjgill

        can you be more specific ? thanks

    • Ankurdalal29

      I also need help, please!
      I am also using two different functionality which is following:
      1) font picker
      2) color picker
      Both not working simultaneously.
      I had tried “noConflict()” but still not working.

  • alamin_cseruet

    I have use two different jquery library in same page action. jquery 1.2.3 work one and jquery Version 1.4.2 work another. if i use only 1.4.2 then other does not work.it happen conflict.how to solve it?

  • Pinkal Patel

    i m use jquery.js File for Easy Slider(For image sliding) and and jquery.min.js for scrolling text.
    Both not working simultaneously.
    I had tried “noConflict()” but still not working.

  • livedefinition_ldtv

    I'm using jQuery 1.4.2 and every thing is working great until I add the “Disqus” forum…:-)
    When I add it, I get a conflict with the jQuery file (how do I know the problem is over there? because when I remove the jQuery file the forum is working, but nothing else is not working obviously) any one know how to work both of them together? i've tried noConflict(), that is why I am here….:-)

    • Sjgill

      Did you figure this out I'm struggling with the same issue right now

      • Erezc

        Yep, use 1.4.4! 1.4.2 has a bug apperently….

  • MitaliSolanki_mscit

    It works but still giving error in jquery is as follow
    $(“input[type='text']:first”, document.forms[0]).focus is not a function

  • MitaliSolanki_mscit

    It works but still giving error in jquery is as follow
    $(“input[type='text']:first”, document.forms[0]).focus is not a function

  • MitaliSolanki_mscit

    It works but still giving error in jquery is as follow
    $(“input[type='text']:first”, document.forms[0]).focus is not a function

  • MitaliSolanki_mscit

    It works but still giving error in jquery is as follow
    $(“input[type='text']:first”, document.forms[0]).focus is not a function

  • MitaliSolanki_mscit

    It works but still giving error in jquery is as follow
    $(“input[type='text']:first”, document.forms[0]).focus is not a function

  • MitaliSolanki_mscit

    It works but still giving error in jquery is as follow
    $(“input[type='text']:first”, document.forms[0]).focus is not a function

  • http://www.facebook.com/profile.php?id=100000625925179 Амангелды Кадыл

    То что надо)

  • Kapil

    Please give some live example.. here is my sample code
    <script type=”text/javascript”>
    jQuery.noConflict();

    jQuery(document).ready(function($){
    jQuery(“#city_dialog”).dialog({
    autoOpen:false,
    width:450
    });
    jQuery(“#ol_link”).click(function($){
    $(“#city_dialog”).dialog(“open”);

    });

    I think something messed up as ther's conflict in prorotypes</script>

  • Jpresch

    mine too!