HI guys, I've just run into an issue. Basically I'm working t a wordpress site, and there are some conflicts with the usage of the "$" sign so I used this in the document ready to allow me to use "$" in place of jQuery:

jQuery(document).ready(function($){ 
    ...
});

It works very well inside the document ready but in my script I have plenty of functions using the "$" and I'd like to be able to use that rather than go through it and replace all $ with jQuery, for example take one of the functions I have outside document ready:

function getNavigationStatus(){//whether navigation is open or closed
    var isNav;
    if($(".navigation").hasClass("open")){
        isNav = true;
    }
    else{
        isNav = false;
    }
    return isNav;
}

I need to be able to use $ rather than jQuery. Any idea (other than replacing every single $ of course)?
thanks

Recommended Answers

All 6 Replies

You could place your functions inside an anonymous functions like so:

(function($) {

    $('#fancy_jquery_element').doFancyStuff();

})(jQuery);

thanks, but the problem was for functions outside document.ready, that's what I meant

I know, that's why I posted the solution above. Look at it again ;). Do you see a document.ready anywhere?

Oh, well, in that case I misread it, sorry. I would have thought that (function($) {})(jQuery); was equivalent to document.ready(function(){}).
The thing is, I have this situation:

document.ready(function(){
//my code
});
function function1(){
//my code
}
function function2(){
//my code
}
function function3(){
//my code
}
...

Does it mean that everything (document ready and functions) can sit inside (function($) {})(jQuery);?

Yes it does.

(function($) {

    // Code

})(jQuery);

simply defines an anonymous function and executes it right away, passing it jQuery as argument for $. You can put everything inside there.

Ah OK, that's great, thanks :-)!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.