This is really to do with how JavaScript handles scope, not something
jQuery has control over.

In short:

If you DON'T use the "var" keyword in front of variable you assign
values to, that variable becomes a "global" variable.

If you DO use the "var" keyword, the variable becomes "local" to the
enclosing function. Unless there's no enclosing function, in which
case it is a global.

Functions in JavaScript are "closures" of a sort.

For more information I suggest doing a search on "javascript scope",
the first result from Google is:

    Scope in JavaScript
    http://www.digital-web.com/articles/scope_in_javascript/

It looks like a fairly comprehensive explanation of the topic.

You can also read up on "closures", first in Google results:

    JavaScript Closures
    http://www.jibbering.com/faq/faq_notes/closures.html

Karl Rudd

On Tue, Sep 30, 2008 at 12:25 PM, QuickScriptz <[EMAIL PROTECTED]> wrote:
>
> Okay, so first off, I recently started using jQuery, and it is by far
> the best and easiest to use/understand framework that I have ever
> used. Huge props to the developers and the community! You guys rock!
>
> Anyway, I've read over the jQuery Documentation but I am left a bit
> fuzzy as to the whole idea of the scope of variables within functions.
>
> Say I have a variable (x) inside a for loop which is inside my $
> (document).ready. If I want to create a function inside my for loop
> (like a mouseover event), is it possible to access (x) from inside
> this function?
>
> Here is an example to clarify:
> --------------------------------------------------------------------
> $(document).ready(function(){
>
>                // Loop it five times
>                for(var i = 1; i <= 5; i++){
>
>                        // Declaring the variable
>                        x = 0;
>
>                        $(".fade").mouseover(function(){
>                                $(this).fadeTo(10, x);
>                        })
>                }
> }
> --------------------------------------------------------------------
>
> Is there some type of shortcode or easy way to access the value of (x)
> from within the mouseover function?
>

Reply via email to