Sunday, February 9, 2014

  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
share|improve this question
1 
Make sure you're including the jQuery libary before your custom script, and that there are no errors in theNet tab of Firebug (make sure jQuery actually gets loaded). –  wsanville Nov 2 '11 at 2:14
 
... yes, of course jQuery is loading in before hand. I'm pretty sure it's a WordPress enque issue. – dcolumbus Nov 2 '11 at 2:37
add comment

4 Answers

up vote50down voteaccepted
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));
share|improve this answer
 
I'm not quite understanding where/how I would apply that script... –  dcolumbus Nov 2 '11 at 2:38
3 
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 2 '11 at 3: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 2 '11 at 4:09 
 
Yes, that is what I was suggesting - "within the function, $ points to jQuery as you would expect". –  El YoboNov 2 '11 at 4:48
 
Another way is !function( $ ) { //code }( window.jQuery ) –  Air Feb 7 '13 at 2:48
show 3 more comments
http://api.jquery.com/jQuery.noConflict/ this url 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
share|improve this answer
add comment

No comments: