Web Log

how to use jQuery in WordPress06.3.11

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.
}

Filed In: Blog, Nerdy
Series: code, how-to, JavaScript, jQuery, Wordpress