On 17 Dez., 08:31, Micky Hulse <[EMAIL PROTECTED]> wrote:
> Hi again,
>
> So, yes! The code works! Many thanks for the tips. :)
>
> In your last example, I am just wondering about the $ (dollar sign)
> before the "foo" variable. When should I use that on variables? I
> assume that I would do this if I am going to use that variable to
> reference an element... For example, is this correct usage/syntax:
>
> ...<snip>...
>
> // Start closure:
> $(function() {
>
> var fooString = 'Just a string'; // <----- No dollar sign. Correct?
> var $foo = $('#foo'); // Test ele, which use the $.
> var $grfkldr = $('<img src="ajax-loader.gif">'); // Loader gif,
> injected into DOM on document ready.
> var $ldr = $('.loading'); // Loading container class.
>
> $grfkldr.appendTo($ldr); // <----- Inject the loader gif. Is this
> correct usage of the syntax?
>
> $(window).load(function () {
> // ... Do something with $foo
>
> });
> });

Hi Micky, it became sort of convention to use a "$" for variables that
represent a jQuery object...:

var $this = $(this);
var $img = $('<img src="..." alt="...">');
var $divs = $('div');

and so on... the examples you were giving look correct (although it
may be hard to remember what a variable named "$grfkldr" stands for if
you need to debug it 2 month later - and don't forget about your co-
workers - but that's up to you of course).

You're not required to do it, in the end it's just a matter of
personal preference. But it can be very useful. Imagine a longer
script with quite a lot variables. If there's a variable $foo then you
immediatly know without looking it up, that it is a jQuery object and
you can apply some jQuery methods to it.


--Klaus







Reply via email to