Home > Development, WordPress > jQuery's no-conflict mode: yet another reason why it's the best

jQuery's no-conflict mode: yet another reason why it's the best

It took me a bit to find out why jQuery (now bundled with WordPress) was not working as I expected inside of the WP admin area. The script was being called, but my code like $("#foo") was not working. I really had no clue where to begin, since it still has all those old JS libraries/frameworks being called as well. It was due to Prototype being packaged with it and conflicting with the "$" shortcut.

Long story short, jQuery already planned for library conflicts and has a quick solution. The no-conflict mode allows you to define your own shortcut, and of course it works like a charm.

It's easy to doΒ - just put this line in your code somewhere:

varΒ J = jQuery.noConflict();

Now $("#foo") will be J("#foo") and it will not conflict with any other libraries that may be installed. I hope WP gets rid of all the other stuff and goes with pure jQuery and plugins soon enough though πŸ™‚

Categories: Development, WordPress
  1. low
    September 1st, 2007 at 16:23 | #1

    excellent solution! worked like a charm! thank you!

  2. mike
    June 11th, 2008 at 20:56 | #2

    Can I see your html, I don't understand where to put it. I am not to good with javascript.
    Thanks

  3. David MorΓ³n
    November 7th, 2008 at 13:17 | #3

    This also fixes the issue between lightbox2 and jQuery Multi Level CSS Menu #2...

    Genious

  4. hubba
    November 8th, 2008 at 12:56 | #4

    THIS ROCKS! I been starring at my wordpress theme code for days.. I know it must be some conflict going on there.. and YOU .. YOU GAVE ME THE ANSWER! this is so awesome!

  5. mei
    November 19th, 2008 at 23:52 | #5

    Sorry for being stupid. But I tried the noConflict() function. I placed it RIGHT AFTER jQuery.js is called, but BEFORE prototype is called. In between these two, I had other UI libraries being called as well. I assigned jQuery to new var $j, and I changed the call in my function (which is called after all the libraries) to refer to $j instead of $, it still doesn't work.

    It looks like my UI libraries functions are now broken (from the error I am getting like "the.style" is null or not an object).

    Can you please help?? Thanks so much in advance!

  6. mike
    November 20th, 2008 at 00:09 | #6

    Honestly, I don't think I can help. I got lucky with this a while ago, and I simply done have the mental patience to try and work with it again... sorry πŸ™‚

  7. Dhaval Patel
    February 2nd, 2009 at 02:51 | #7

    Thanks for this great solution.
    But I have one problem with this.
    I am using "jquery.blockUI.js" also.
    I have put "var JQ = jQuery.noConflict();"
    I changed all "$" with "JQ" in "jquery.blockUI.js"
    but as I use "JQ.blockUI({.......});", I am getting the javascript error that "JQ.blockUI is not a function"...

    So what should I do to make working ?

  8. mike
    February 2nd, 2009 at 11:10 | #8

    I don't know... I'd try the author of blockUI πŸ™‚

  9. porkproductpete
    March 5th, 2009 at 22:46 | #9

    okay, so i'm running proto/jquery together and it works fine on Safari and FF with out having to add jQuery.noConflict(); but it won't work on IE6/7... separately both scripts work fine in IE6/7. When I open my page in IE6/7 the jquey.js works but the proto/scriptac opens my lightbox in a new page... I tried the jQuery.noConflict(); fix with no results. I'm not sure if i'm implementing it properly...i'm new to this and eager to learn. I also tried moving jquery script above the proto script recommended on other sites and nothing.
    here is the code i need help with. help me obe wan you're my only hope...
    here is the code i need help with.

          var GB_ANIMATION = true;
          $(document).ready(function(){
            $("a.greybox").click(function(){
              var t = this.title || $(this).text() || this.href;
              GB_show(t,this.href,420,450);
              return false;
            });
          });
    
  10. mike
    March 5th, 2009 at 23:51 | #10

    Sorry dude, but I've got too much on my plate right now to be able to help. I haven't touched this code in a long time.

    You should try #jQuery on irc.freenode.net ...

  11. meneerbroekhuis
    May 12th, 2009 at 07:01 | #11

    Worked like a charm with mootools and jquery conflict in joomla. I couldn't get virtuemart working, but by simply adding your code "var J = jQuery.noConflict();" all problems disappeared! Many Thanks

  12. klaus
    June 14th, 2009 at 05:56 | #12

    i've benn sitting in front of this problem for hours. i knew that there was a conflict, but i couldnt get it to work, since I read this article.
    thank you very much

  13. Osman Titiz
    July 31st, 2009 at 06:52 | #13

    Thank you very much!!! I have searched a lot I finally found this text :)) Great idea! Thanks..

  14. Anonymous
    August 14th, 2009 at 09:06 | #14

    var J = jQuery.noConflict();

    at the top of your Jquery function;
    then replace all instances of $ with J inside your jQuery function!

    Worked perfectly!

  15. Henry
    August 17th, 2009 at 04:42 | #15

    @Anonymous
    As explained in this post you don't need to replace all your "$" with "J". All you have to do is to use this javascript workaround:

    function($){
      // ur code that uses $
      $('#myId').show();
    }(jQuery);
    
  16. Claudia
    September 21st, 2009 at 06:23 | #16

    Thanx so much!!!! πŸ˜€ worked like a charm! πŸ˜€

  17. Mitch
    September 28th, 2009 at 22:27 | #17

    Where do I place the jquery code? Im have a issue with the jquery in featured content glider. Its conflicting with smash galleries and lightbox-plus, any help please?

    I been trying to find the anwser for weeks.

  18. mike
    September 29th, 2009 at 11:25 | #18

    Look a couple posts up. That's probably the best answer I'd give.

  19. tianos
    October 4th, 2009 at 17:55 | #19

    great, it works.

    When i combined a modal dialog box using facebox.js and a slidehow in a page, it did not work.

    i put this like the script above:

    .......*some jQuery*
    .......

    var J = jQuery.noConflict();

    /*** and then i changed all $ to J in function () ***/

    accomplished

    thanks

  20. Anonymous
    October 7th, 2009 at 12:25 | #20

    I have the same problem as Dhaval Patel
    I am using jQuery.js and dataTable.jQuery.js which conflicts with protoType.js
    the noConflict() works perfect for jQuery, but the dataTable.jQuery.js says function not found and tries to look into protoType.js....
    Any idea to get arround this.. ??

  21. mike
    October 7th, 2009 at 12:29 | #21

    I can't promise i can help but if you pastebin'ed the source code to dataTable file (which should be the place you need to make a change, not in jQuery.js) perhaps someone can help out or I can find the culprit quick. Javascript gives me a headache. Hence why I love jQuery πŸ™‚

  22. Shahar
    October 18th, 2009 at 04:18 | #22

    @mike
    Put it at the top of you .js file and then replace any '$' you've used. for example:

    $(function () {
    // your code here
    }
    

    becomes:

    var J = jQuery.noConflict();
    J(function () {
    // your code here
    }
    

    (Thanks for the post!)

  23. Arno
    November 2nd, 2009 at 22:07 | #23

    Waouhhh!! What's a wonderfull topic, there was a long time I was looking for this solution !!

    Thanks, Thanks and Thanks again!!

    Arno

  24. Samuel
  25. nick
    January 4th, 2010 at 13:18 | #25

    JQuery continaully saves my sanity from javascript embolisms! Thanks for posting this out there.

  26. Sun
    January 19th, 2010 at 06:57 | #26

    Great posting. Thank you so much!!! It works great!!

  27. zeljic
    January 26th, 2010 at 16:32 | #27

    Worked perfectly!
    Thank you...

  28. Alexander
    February 5th, 2010 at 04:54 | #28

    Hello, when I use this solution IE 8 crashes. The other browser work fine.

  29. Eep
    February 19th, 2010 at 04:01 | #29

    Great, I had conflict between two plugins in WordPress (Featured Content Galary and WPReviewengine) and put your line in jquery.js. SOLVED. thx

  30. siva
    March 1st, 2010 at 21:22 | #30

    u r the real genious man.

  31. Rob
    March 10th, 2010 at 12:37 | #31

    Hey,

    Thanks for this solution, I wrote a small script based on the jTwitter library to fetch my latest tweets and it was conflicting with LightBox, but this solved it πŸ™‚

    Cheers!

  32. KIran
    March 25th, 2010 at 04:13 | #32

    Hi,

    Thank you very much for your idea.

    Worked perfectly!

  33. CMentality
    April 27th, 2010 at 14:22 | #33

    Hey,

    Thanks! I was looking for a solution like this.
    8)

  34. Gillian
    July 2nd, 2010 at 17:09 | #34

    OK, well! Thanks it worked for me, at last, I was missing all of the other $ in the code. I'm using proto for lightbox and jquery for paging

    My code before adding ".noConflict"

            var J = jQuery.noConflict();
                // This demo shows how to paginate elements that were loaded via AJAX
                // It's very similar to the static demo.
            
                /**
                 * Callback function that displays the content.
                 *
                 * Gets called every time the user clicks on a pagination link.
                 *
                 * @param {int}page_index New Page index
                 * @param {jQuery} jq the container with the pagination links as a jQuery object
                 */
    			function pageselectCallback(page_index, jq){
                    var new_content = $('#hiddenresult div.result:eq('+page_index+')').clone();
                    $('#Searchresult').empty().append(new_content);
                    return false;
                }
               
                /** 
                 * Callback function for the AJAX content loader.
                 */
                function initPagination() {
                    var num_entries = $('#hiddenresult div.result').length;
                    // Create pagination element
                    $("#Pagination").pagination(num_entries, {
                        num_edge_entries: 2,
                        num_display_entries: 8,
                        callback: pageselectCallback,
                        items_per_page:1
                    });
                 }
                        
                // Load HTML snippet with AJAX and insert it into the Hiddenresult element
                // When the HTML has loaded, call initPagination to paginate the elements        
                $(document).ready(function(){      
                    $('#hiddenresult').load('snippet.html', null, initPagination);
                });
    

    Code after adding .noConflict, notice the use of J instead of $, which occurs 6 times.

    (jquery was placed before prototype)

            var J = jQuery.noConflict();
                // This demo shows how to paginate elements that were loaded via AJAX
                // It's very similar to the static demo.
            
                /**
                 * Callback function that displays the content.
                 *
                 * Gets called every time the user clicks on a pagination link.
                 *
                 * @param {int}page_index New Page index
                 * @param {jQuery} jq the container with the pagination links as a jQuery object
                 */
    			function pageselectCallback(page_index, jq){
                    var new_content = J('#hiddenresult div.result:eq('+page_index+')').clone();
                    J('#Searchresult').empty().append(new_content);
                    return false;
                }
               
                /** 
                 * Callback function for the AJAX content loader.
                 */
                function initPagination() {
                    var num_entries = J('#hiddenresult div.result').length;
                    // Create pagination element
                    J("#Pagination").pagination(num_entries, {
                        num_edge_entries: 2,
                        num_display_entries: 8,
                        callback: pageselectCallback,
                        items_per_page:1
                    });
                 }
                        
                // Load HTML snippet with AJAX and insert it into the Hiddenresult element
                // When the HTML has loaded, call initPagination to paginate the elements        
                J(document).ready(function(){      
                    J('#hiddenresult').load('snippet.html', null, initPagination);
                });
    

    Thanks again Mike

  35. Gillian
    July 2nd, 2010 at 17:12 | #35

    sorry, the 1st code without noConflict, shouldn't have the noConflict in the start! cut n paste job, sos

  36. Hugo Leon
    September 1st, 2010 at 14:05 | #36

    I spent almost 4 hours with this problem. Now it works!

    Thanks a lot!

    Hugo

  37. funzool
    October 17th, 2010 at 23:51 | #37

    thanks alot! been reading jquery doc but didnt understand. it's clearer here. my prob has been solved πŸ™‚

  38. mozzle
    November 5th, 2010 at 16:34 | #38

    Thank you!!! I'm pretty new to jQuery and this issue was driving me nuts!

    Can't thank you enough for the solution.

    mozzle

  39. LJLee
    November 10th, 2010 at 08:37 | #39

    Excellent !! Thank you so much for the sharing!!

  40. craig
    November 14th, 2010 at 01:06 | #40

    Thanks for posting. I had been trying to integrate a jquery slideshow into my magento frontpage and had found the conflict with prorotype.

    I had read many of the no conflict workarounds but just couldn't get them to work then I found and tried yours and I now have my navigation back.

    If people were to share their coding successes, no matter how small, it would help so much

  41. mike
    November 14th, 2010 at 01:18 | #41

    @craig
    That's what I try to do. If I can document something clearly and help someone else out, hopefully it will get out there for other people to benefit from!

  42. StuRB
    April 25th, 2016 at 10:54 | #42

    @Anonymous
    Works perfectly, thanks. For anyone not clear, it has been said above, but you basically have to:

    1. Add this line to the top of your script section that first uses jQuery:
    var J = jQuery.noConflict();

    2. Replace $ with J, e.g.
    $("#mysearch").autocomplete becomes J("#mysearch").autocomplete

    Note, I only had a small bit of code using jQuery, so this was easy. The rest of my code used prototype.js, which I could leave alone. The reason you might want to use two libraries is if you are meshing two different things together. If you are starting afresh it would be best to avoid the clash in the first place.

    StuRB

  1. July 29th, 2007 at 13:56 | #1
    Around the web | alexking.org
  2. August 21st, 2009 at 14:12 | #2
    Javascript libraries working together | Oikofugic Design
  3. April 21st, 2010 at 03:50 | #3
    jquery und mootools - TYPO3 Forum & Portal
  4. April 27th, 2010 at 01:26 | #4
    ??jQuery??????? | ????
You must be logged in to post a comment.