Wordpress how to use jquery and $ sign
By default when you enqueue jQuery in Wordpress you must use jQuery
, and $
is not used (this is for compatibility with other libraries).
Your solution of wrapping it in function
will work fine, or you can load jQuery some other way (but that's probably not a good idea in Wordpress).
If you must use document.ready
, you can actually pass $
into the function call:
jQuery(function ($) { ...
This should fix it:
jQuery(document).ready(function($){
//you can now use $ as your jQuery object.
var body = $( 'body' );
});
Put simply, WordPress runs their own scripting before you can and they release the $
var so it won't collide with other libraries. This makes total sense, as WordPress is used for all kinds of web sites, apps, and of course, blogs.
From their documentation:
The jQuery library included with WordPress is set to the noConflict() mode (see wp-includes/js/jquery/jquery.js). This is to prevent compatibility problems with other JavaScript libraries that WordPress can link.
In the noConflict() mode, the global $ shortcut for jQuery is not available...