Flag This Hub

How to use jQuery's .noConflict() functionality

By


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

hathibelagal 2 years ago

Short.. but useful.

MUHAMMAD SALMAN 16 months 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 13 months 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 12 months 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 9 months 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 8 months ago

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

Johnpantooo 8 months 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://uzbekjon.hubpages.com/hub/How-to-use-jQuery

Thanks !!!!!

gangeshvar 5 months 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 dont 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 8 weeks ago

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

dasdada 2 weeks ago

dsada

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working