Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am fairly new to using jQuery in WordPress and I am trying to write jQuery scripts in a separate file.

What is the difference between the following two syntaxes of setting up the external file:

Version I

jQuery(function() {
    jQuery("#radio").buttonset();
});

Version II

$(document).ready(function() {
    $("#radio").buttonset();
}
share|improve this question

closed as off topic by kaiser, s_ha_dum, Johannes Pille, brasofilo, Wyck Mar 20 '13 at 16:23

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
whats WordPress got to do with this question? –  Bainternet Mar 20 '13 at 11:17
1  
Agreed, @Bainternet - it ain't WP specific at all. However, if you encounter noConflict mode for the very first time with WP, I can see why it would seem as if it was. –  Johannes Pille Mar 20 '13 at 11:26
add comment

1 Answer 1

jQuery.noConflict()

jQuery is included in WordPress in noConflict mode, so as to work with other javascript extensions that also use $ as an alias.

Hence your above Version II will not work with the WP native jQuery loading.

What is jQuery?

jQuery is not a language. It is nothing but a (massive) javascript object. That object's name is jQuery. When .noConflict() is not applied, $ is an alias for the jQuery object.

Best practice for external files

Wrap the entire js in a closure, pass it the jQuery object and $ as an argument. Inside you can write your script the way you're used to:

(function($){
    $(document).ready(function() {
        $("#radio").buttonset();
    }
})(jQuery);
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.