The dollar-sign
($
) in Javascript has for many years now been associated with jQuery.
When you see this error:
“uncaught typeerror: $ is not a function ”
It almost always means that a jQuery piece of code is being run before the jQuery library has been initialised and loaded.
Oftentimes, your code will wait for jQuery to load before continuing any processing, this is almost always done as follows:
$(document).ready(function(){
// jQuery code is in here
});
However, it’s not uncommon to get an “TypeError: $ is not a function ” error in response.
Some developers resolve this by wrapping their code as follows:
(function($){
// jQuery code is in here
})(jQuery);
What this does is, it forces a scoped jQuery
variable to be re-aliased within an anonymous function in your application.
Alternatively, you can just do the following:
jQuery(document).ready(function($){
//you can now use $ as your jQuery object.
var body = $( 'body' );
});