jQuery and WordPress: Using noConflict

by Justin on August 18, 2009

I have been using wordpress for a while and now I’m starting to integrate more jQuery into some of my blogs. Problem is I have been having problems with the code acting up. After searching I found the jQuery is now included in the core code for wordpress. More reading and I found that I need to use jQuery’s noConflict to make my code work in wordpress. Using noConflict your code will look like this:

1
2
3
4
5
6
7
8
$j = jQuery.noConflict();

$j(function() {
        $j("#sidebar #trigger").click(function(event) {
                event.preventDefault();
                $j("#box").slideToggle();
        });
});

Basically anywhere you would have used just the $ sign you add in a “j”. The variable name can be anything some examples use $js I just chose $j.

Please sign up for my feed as I will be adding more wordpress and jQuery goodness!

{ 2 comments… read them below or add one }

donnie July 28, 2010 at 10:42 am

It took me several failed jquery projects before I got a handle on how to get it working conflict free in Wordpress. I wish I had found this tip sooner.
.-= donnie´s last blog ..SEO TIP- Goodbye PageRank- Hello Trust and Authority =-.

Reply

Justin September 2, 2010 at 10:36 am

Thanks Michael. Hopefully this will save you some debugging time.

Reply

Leave a Comment

Previous post:

Next post: