jQuery: $ is not a function

December 30th, 2008
28 comments JQuery, WordPress posted by Pete

Recently I ran into an issue  while trying to use jQuery on a custom WordPress based Content Management System.  This particular system had a number of instances in which the framework was being referenced.  Normally you insert jQuery code using the dollar sign ($) like so:

$(document).ready(function() {
   $("a").click(function() {
     alert("Hello world!");
   });
 });

The problem arises when a different system grabs the $ variable.  All of the sudden you have multiple $ variables being used as objects from multiple libraries, resulting in the error console message “$ is not a function”.

Fortunately there is a pretty easy way of fixing this in the form of jQuery.noConflict.  Running this function gives control of the $ variable back to whichever library first implemented it.  This will help to ensure that jQuery won’t conflict with other instances of the $ object in other libraries.

But please note, in doing this, you are re-assigning the variable so you will only be able to access jQuery commands using the ‘jQuery’ variable (which has just replaced ‘$’).  So our above code example would look something like this:

jQuery.noConflict();
jQuery(document).ready(function() {
   jQuery("a").click(function() {
     alert("Hello world!");
   });
 });

It doesn’t seem to be a very common problem and only occurs when multiple instances of the object crop up, but that’s easy to do in WordPress when multiple plugins begin conflicting with each other.   Follow the link for the  full jQuery documentation on jQuery.noConflict

  1. ravi said on 10/11/09

    Thanks Man! very useful article.

  2. envio said on 11/03/09

    Thank’s , I am newbie on jQuery ^^

  3. Pete said on 11/03/09

    No problem, just glad it helped :-) And Envio, we all gotta start somewhere, eh?

  4. Vincy said on 12/02/09

    Very useful article. Thank you!

  5. Mike said on 12/19/09

    Dude, you just saved me. I couldn’t figure out what was wrong with my code.

  6. Mads said on 02/04/10

    var $ = jQuery.noConflict();
    $(function){…

    Also works :)

  7. css said on 02/20/10

    Nice positing

  8. Esteban said on 04/06/10

    Hi!

    I stumbled upon this article: http://codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems/ and I was wondering what’s wrong with this solution:

    function($) {
       // we can now rely on $ within the safety of our “bodyguard” function
       $(document).ready( function() {
           alert(”nyah nyah! I’m able to use ‘$’!!!!”);
       } );
    } ) ( jQuery );
    
  9. Pete said on 04/06/10

    Hi Esteban.

    Is that function not working for you? If its working for your particular space, there should be nothing whatsoever wrong with using it.

    As Mads demonstrated above, one of the great things about programming / problem solving is that there is generally many ways to accomplish a problem.

  10. Nicolo Verrini said on 04/16/10

    Thank you Man

  11. Pete said on 04/16/10

    No problem Nicolo ;-) Just glad it helped.

  12. Hasan said on 08/10/10

    Very useful article thanks alot man……

  13. vestes Moncler said on 10/20/10

    merci beaucoup pour cette information, Merci pour un autre blog d’information , maintenir le bon travail !

  14. Anything Graphic said on 11/23/10

    I have been messing with this for so long and finally gave up. Just revisited the situation and you helped make my jQuery work.

    Thank you SO much! UGH… :-)

  15. fueradellimite said on 12/21/10

    it works for me!

  16. Shane said on 04/06/11

    This was a great help in getting some jquery running on a Drupal 7 site, looks like something else had grabbed “$” as a variable.

    I used Mads’s method of
    var $ = jQuery.noConflict();
    and it worked fine.

    Thanks for the info!

  17. Shanee said on 05/23/11

    Hi;

    I cannot identify what’s wrong in my code.It’s functions are not working.The below one is my code.

    var $ =jQuery.noConflict();
    $(document).ready(function(){
    $(“button”).click(function(){
    $(“p”).hide();
    });
    });

    This is a heading
    This is a paragraph.
    This is another paragraph.
    Click Me

  18. Jonathan Dingman said on 05/28/11

    Perfect! Thank you for this simple, yet very effective post.

    I had this same problem and this helped me solve it:

    var $ = jQuery.noConflict();

    thank you!

  19. Subhan said on 05/30/11

    This post really helped me a lot. I thank everybody here from the bottom of my heart :D

  20. Mike Higginbottom said on 06/30/11

    Brilliant. I was struggling to get jPlayer running in Drupal 7 until Firebug popped up this error message. Thanks for pointing out the solution.

  21. vani said on 07/25/11

    Thanks alot.It worked just fine

  22. Alexis said on 07/31/11

    Ahhhh man I’ve been wondering that for ages, I kept following the examples on the main site incojunction with slider from dynamic drive, I’ve been scratching my head as to why $ wasn’t working (I wont say how long for)

    Cheers!

  23. Rich Jones said on 08/12/11

    If this tip isn’t working for you, try this replacing all your $()’s with jQuery().

  24. pankaj said on 08/26/11

    Didnt notice it, but I had the same issue and decided to write an article on this which came out quite similar to this one :)
    http://techinfogurus.com/modules/editor/view_article.php?id=18

  25. sujit said on 09/16/11

    was really messed up with my jquery, it really worked for me…Great work.. Thanks a lot!!!!

  26. Charl said on 10/28/11

    Thanks for sharing – made my day!

  27. Dan said on 10/31/11

    awesome

  28. charith said on 11/10/11

    Thank you very much

Leave a Reply