SouthCoaster
SouthCoaster

Reputation: 51

what is the difference between jQuery(function($) and $(function()?

I'm looking at a piece of code I did not write which contains:

jQuery(function($) {

$('#interaction').find('.item').hover(function() {
    var $this = $(this);
    $this.addClass('hover');
},
function() {
    var $this = $(this);
    $this.removeClass('hover');
})
.click(function() {
    var $this = $(this);
    var thisID = $this.attr('id');
    //hide all visiable detail pages
    resetpage($('.item-detail:visible'));

... etc.

Normally I would write my code to run inside of $(document).ready({ ... }); for example:

$(document).ready({

    .click(function() {
        var $this = $(this);
        var thisID = $this.attr('id');
        //hide all visiable detail pages
        resetpage($('.item-detail:visible'));
        ... etc.

    }
});

What is the difference (if any) between these two ways of writing the function or can I use them interchangeably?

Upvotes: 2

Views: 1032

Answers (1)

ahren
ahren

Reputation: 16961

You can use them interchangeably. $ is shorthand for jQuery, and $(function(){..}) is shorthand for $(document).ready(function(){ });

Sometimes people use jQuery(function($){ }); because the $ symbol is used by another library, or conflicts with PHP on the server.

Upvotes: 4

Related Questions