142
  1. I'm using a WordPress site.
  2. I'm including this script in the header.

When the script loads, I get this error:

TypeError: 'undefined' is not a function (evaluating '$(document)')

I have no idea what is causing it or what it even means.

In firebug, I get this:

$ is not a function

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
dcolumbus
  • 9,596
  • 26
  • 100
  • 165
  • 4
    Make sure you're including the jQuery libary **before** your custom script, and that there are no errors in the `Net` tab of Firebug (make sure jQuery actually gets loaded). – wsanville Nov 02 '11 at 02:14
  • ... yes, of course jQuery is loading in before hand. I'm pretty sure it's a WordPress enque issue. – dcolumbus Nov 02 '11 at 02:37

14 Answers14

241

Wordpress uses jQuery in noConflict mode by default. You need to reference it using jQuery as the variable name, not $, e.g. use

jQuery(document);

instead of

$(document);

You can easily wrap this up in a self executing function so that $ refers to jQuery again (and avoids polluting the global namespace as well), e.g.

(function ($) {
   $(document);
}(jQuery));
El Yobo
  • 14,823
  • 5
  • 60
  • 78
  • 5
    The script is not necessary; you can just call `jQuery(document)` instead of `$(document)` wherever you want. If you do want to use `$` instead, then the above script simply creates a function which takes an argument `$` then executes it passing `jQuery`; in short, within the function, `$` points to jQuery as you would expect. – El Yobo Nov 02 '11 at 03:08
  • 2
    Actually, what I did was place my normal `$(document).ready(function() { // code });` in place of your `$(document);` ... that worked like a charm. – dcolumbus Nov 02 '11 at 04:09
  • 1
    Yes, that is what I was suggesting - "within the function, $ points to jQuery as you would expect". – El Yobo Nov 02 '11 at 04:48
  • Well, it does save you a couple of characters that way :) More details about [the !function(){}() form of self executing statement](http://stackoverflow.com/questions/5422585/preceding-function-in-javascript). – El Yobo Feb 07 '13 at 20:24
  • I know it's old @ElYobo, but I think you could edit your post and add your example from your first comment into the response as a second example. Thank you for saving me time =) – Manatax May 29 '13 at 00:18
  • undefined is not a function can also be caused by including jQuery more than once – Michael L Watson Jun 13 '14 at 06:00
  • For those using Jenzabar, this is the solution you're looking for as well. – Riot Goes Woof Aug 12 '14 at 14:52
28

Use jQuery's noConflict. It did wonders for me

var example=jQuery.noConflict();
example(function(){
example('div#rift_connect').click(function(){
    example('span#resultado').text("Hello, dude!");
    });
});

That is, assuming you included jQuery on your HTML

<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
Ram
  • 3,092
  • 10
  • 40
  • 56
Leno Britto
  • 281
  • 3
  • 2
16

Use this:

var $ =jQuery.noConflict();
slavoo
  • 5,798
  • 64
  • 37
  • 39
Suman Biswas
  • 853
  • 10
  • 19
11

Try this snippet:

jQuery(function($) {
  // Your code.
})

It worked for me, maybe it will help you too.

hugronaphor
  • 948
  • 8
  • 23
Gurpreet Dhanoa
  • 131
  • 1
  • 2
11

I had this problem only on Chrome.

I tried adding

var $ =jQuery.noConflict();

just before calling

$(document).ready(function () {

It worked.

Thanks a lot

Stewartside
  • 20,378
  • 12
  • 60
  • 81
Le_Dhul
  • 121
  • 1
  • 2
  • Thanks for this one. I knew I could resolve the issue using "jQuery" instead of "$" but my problem was that it trips up Komodo edit when written that way so I had to choose between getting code hinting/completion on $ or having my JavaScript actually work. This method let me have both. – George Kendros Jun 15 '15 at 20:36
6

Also check for including jQuery, followed by some components/other libraries (like jQuery UI) and then accidentially including jQuery again - this will redefine jQuery and drop the component helpers (like .datepicker) off the instance.

agrath
  • 901
  • 1
  • 11
  • 25
5

I would use this

(function ($) {
   $(document);
}(jQuery));
Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
JoT
  • 301
  • 1
  • 3
  • 10
4
 ;(function($){
        // your code
    })(jQuery);

Place your js code inside the closure above , it should solve the problem.

Bikram Shrestha
  • 2,010
  • 24
  • 22
2

I have also faced such problems many time in web develoment carrier. Actually its not JS conflict, When we load html website to the browser we face no such error, but when we load these type of website through localhost we face such problem. That's because of localhost. When we load scripts through the localhost it scans the script and renders the output. But when we didn't use localhost. It just scan the output. Example, when we write php code putside the localhost and run without host we get error. But if the code is correct and is run through host we get actual output, and when we use inspect element we get the output code in HTMl format but not in PHP format this is because of rendering of the code.

This is rendering error. So to fix these jquery code error one of the solution may be using this method.

jQuery(document).ready(function($){
/******** Body of Jquery Code*****/
});

What this code does is register '$' as the varible to the function by applying jquery. Else by default the .js file is only read as javascript.

Rajan Lama
  • 21
  • 3
1

I ran into this problem also when including jQuery in my page header, not realizing the host was already including it in the page automatically. So load your page live and check the source to see if jQuery is being linked in.

FrostyL
  • 811
  • 6
  • 9
1

Two things:

  1. Be sure that you have jQuery library added, before your $(document).
  2. Then just change all "$" with: jQuery , as the previous comments.
1

wrap all the script between this...

<script>
    $.noConflict();
    jQuery( document ).ready(function( $ ) {
      // Code that uses jQuery's $ can follow here.
    });
</script>

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 you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $.noConflict(). Old references of $ are saved during jQuery initialization; noConflict() simply restores them.

Jitendra Damor
  • 774
  • 17
  • 27
1

You can use both jQuery and $ in below snippet. it worked for me

jQuery( document ).ready(function( $ ) {
  // jQuery(document)
  // $(document)
});
Kalpesh Prajapati
  • 1,703
  • 1
  • 12
  • 15
1

You can pass $ in function()

jQuery(document).ready(function($){

// jQuery code is in here

});

or you can replace $(document); with this jQuery(document);

or you can use jQuery.noConflict()

var jq=jQuery.noConflict();
jq(document).ready(function(){  
  jq('selector').show();
});
Ganesh
  • 1,136
  • 13
  • 25