Blog » JavaScript

how to use jQuery in WordPress June 3, 2011

Depending on motivation, these “how-to’s” may or may not become a regular occurrence. I get asked web questions all the time and instead of having to explain the same thing over and over again, I’d like to just start cataloging my solutions.

I’ll kick things off with a quickie:

PROBLEM:
You’re trying to use jQuery in your WordPress theme, widget or plugin but you keep getting an error that’s something along the lines of “Result of expression ‘$’ [undefined].”

DIAGNOSIS:
Chances are, there are other JavaScript libraries being loaded that also uses “$” and it’s fucking up your juice.

SOLUTION:
Use “.noConflict()” to get your party started.

$j=jQuery.noConflict();
$j(document).ready(function($){
// Code that uses jQuery's regular "$" can hang out in here.
}