How to use jQuery's .noConflict() functionality

About jQuery.noConflict();

jQuery is a great javascript library that makes your javascript development more interestin, exciting and effective. Most of us use jQuery without even knowing how it works or how to write jQuery code. We just take jQuery plugin and plug it in into our web pages. We go so crazy that we don't just plug in jQuery plugins, but also end up with MooTools, prototype.js plugins in our pages and that's probably when the problems start. All those plugins work perfect on their own but when you add another javascript framework plugin they start to conflict.

The good news is that jQuery made sure it does not conflict with other plugins. All you need to use is jQuery's .noConflict() method like this jQuery.noConflict(); before you write any jQuery code.

Example use

Here is an exaple of its use taken from jQuery.noConflict() post on jQuery blog:

<html>
  <head>
  <script src="prototype.js"></script>
  <script src="jquery.js"></script>
  <script>
    jQuery.noConflict();
    // Use jQuery via jQuery(...)
    jQuery(document).ready(function(){
        jQuery("div").hide();
    });
    // Use Prototype with $(...), etc.
    $('someid').hide();
  </script>
  </head>
  <body></body>
</html>
 

When you call .noConflict() jQuery will return $() to it’s previous owner and you will need to use jQuery() instead of shorthand $() function.

You can also use the following code snippets to still use $() in your code, but with one drawback, you will not have access to your other library’s $() method.

// Method 1
jQuery(document).ready(function($){
    $("div").hide();
});

// Method 2
(function($) {
    /* some code that uses $ */ 
})(jQuery);


TIP: Don’t forget that you can always assign jQuery to any other variable name to use it as your shorthand: var $_ = jQuery;

Comments 11 comments

hathibelagal profile image

hathibelagal 7 years ago from A jungle inn

Short.. but useful.


MUHAMMAD SALMAN 5 years ago

Hi , this is very informative

But i have a problem i am using accordin library with yui when i use jQuery.noConfilt() , my problem does not remove

plz help

thxx


Abdelkader Soudani 5 years ago

Absolutely right! sometimes you get too excited implementing lots of Jquery functions and out of nowhere something makes conflict and oops... nothing works anymore!

Thanks for the great post, very informative and straight forward, I've enjoyed it.


Akhmad 5 years ago

@MUHAMMAD SALMAN AND @Abdelkader Soudani

I use "jQuery" to variable selector and it works, eg. $(document).ready(function(){

});

to

jQuery(document).ready(function(){

});

Thanks!


Nipu 5 years ago

Thanks for share this but i have a problem i am using mootools when i use jQuery.noConfilt() , my problem does not solve.


Darren 5 years ago

Cheers, any help on this tho: http://freetemplatez.net/


Johnpantooo 4 years ago

Hello Everyone,

In JQuery generally ($) dollar sign is an indication of using JQuery function but jQuery give us functionality to rename ($) dollar sign instead of other user define variable name. By using noConflict () method.............. for more details please check out this link.

http://hubpages.com/technology/How-to-use-jQuery_n...

Thanks !!!!!


gangeshvar 4 years ago

Hi, I was wondering if you could help me with a conflict on my site.

I have a menu that uses jQuery, and a script (LightView) running prototyp.

Ive researched the conflict, and tried to understand how to fix it, but I'm pretty much lost.

I don't know where to properly use .noConflict()

Could you take a look at mysite and maybe suggest a solution?

My email is squirrelslax[at]gmail.com

thank you


tj 4 years ago

hye is page kinda old but isit still alive.cuz i need help :)


dasdada 4 years ago

dsada


munna 2 years ago

after using noconflict, if we add highcharts library dynamically it is giving object expected error.

can you please tell me how to avoid this error.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working