0

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

Edited by Violet_82

2
Contributors
6
Replies
25
Views
1 Year
Discussion Span
Last Post by Violet_82
2

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

(function($) {

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

})(jQuery);
0

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

0

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

0

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);?

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.