5. Using functions to select HTML elements


#1

$(document).ready(function() {
    $fade = $('div');
    $fade.fadeIn('slow');
    });

$(document).ready(function() {
    $('div').fadeIn('slow');
    });

Regarding these two bits of code, if I had to make a div fade, which would be better?

Thanks


#2

Hi Tyoungg,

There's not always a ‘better’ version of code. But, in this case, I'd go with the second unless you need to do other stuff with the <div> later on in your code. If you're doing more than a few things with something, storing it in a variable is often a good idea :slightly_smiling:


#3

Thanks for the reply!