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

dcolumbus picture dcolumbus · Nov 2, 2011 · Viewed 335.7k times · Source
  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

Answer

El Yobo picture El Yobo · Nov 2, 2011

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));