If you’re working on a WordPress-based project that leverages jQuery, odds are you’ve noticed that the library is included in noConflict mode such that the ‘$’ function shortcut is not available.
This is a smart primarily because it prevents future developer’s work from conflicting with what ships with the application; however, if you’re content to work with the library as is (versus including a newer version), you may want to take advantage of the shortcut function.
Here’s how:
In your script, wrap your code in the ready function and pass the shortcut function in as a parameter.
For example:
[cc lang=”javascript”]
jQuery(function($) {
// You code here. ‘$’ can now be used.
});
[/cc]
This will allow you to use ‘$’ rather than jQuery throughout any code contained within the ready function.
Done and done. Easy enough.
Jacob Gillespie says
Great tip… thanks!
If you’re wanting to use jQuery someplace other than the document.ready function, you can use a JavaScript closure like so:
function($) {
// use $ in here
}(jQuery);
Tom McFarlin says
Totally – thanks for posting this :).