1

i wanted to know difference between both.

(function($){
    //some console.log code     
});
$(document).ready(function()
{
    //some console.log code
});

You guys might call me stupid but i don't know why its happening.

well here is problem.

When i use (function($){ then i can't see any result in console.log but it's showing all console debug result when i use document.ready.

I am using jQuery v1.8.2.

Thanks.

Dipesh Parmar
  • 26,601
  • 7
  • 57
  • 86

3 Answers3

2

the first one

$(function(){...});  //missing $ sign here in your code

this is just a shorcut to call document.ready in Jquery.. both is exactly the same.. if you happen to see the core .. you will notice this in a comment...here is the link

bipen
  • 35,563
  • 9
  • 45
  • 62
1

The code

(function($){
    //some console.log code     
});

should be like that

$(function() {
    //some console.log code     
});

Now test it.

Refer http://api.jquery.com/ready/

Rohan Kumar
  • 39,838
  • 11
  • 73
  • 103
1

You missed something at the closing in the first example:

(function($){
    //some console.log code     
})(jQuery); // <----------add (jQuery) here and test it

or this:

  jQuery(function($){ // <---------add jQuery first here
    //some console.log code     
  });
Jai
  • 72,925
  • 12
  • 73
  • 99