0

I am following along in jquery the missing manual and an example says to write it out like this:

$(document).ready(function() {
    $('body').hide().fadeIn(1000);
});

but when I continue reading it it says your code should look like this:

 $(function() {
        $('body').hide().fadeIn(1000);
   });

it drove me crazy because I'm a beginner, they both seem to work but I want to know which is proper

1 Answers1

1

They are identical. The only thing that would make the second one better is that is is less code to type. It is only a shorthand version of the document ready function.

jQuery has a few of these "shortcuts". Another example is the click handler. It can be written in two ways:

$( element ).click( function(){ ... } );

$( element ).on( "click", function(){ ... } );

Going back to the document ready statement, the jQuery documentation has this to say about the two different versions:

Experienced developers sometimes use shorthand for $( document ).ready(). If you are writing code that people who aren't experienced with jQuery may see, it's best to use the long form.

Lix
  • 47,311
  • 12
  • 103
  • 131
  • so '$(document).ready' doesn't need to be there. So as long as the dollar sign is there I can remove '$(document).ready' – Idesignedme Jun 15 '14 at 14:26
  • You shouldn't look at it like a string... It a function call - they *are* different but have the same behavior. But yes - you are correct. – Lix Jun 15 '14 at 14:38