You'll find that you'll likely never have to do a for loop like that
using jQuery. And in your sample the function is being attached to the
mouseover event 5 times.

If you need access to a variable outside the function scope make it a
property of a global object, like:

var QuickScriptz; // store stuff here

$(document).ready(function(){

                // Loop it five times
                for(var i = 1; i <= 5; i++){

                        // Declaring the variable
                        QuickScriptz.x = i;

                        $(".fade").mouseover(function(){
                                $(this).fadeTo(10,QuickScriptz.x);
                        });
                }
});

And then you have a fadeTo() function being called 5 times in a row
for the same element, with different values :)

This might work the way you wanted originally, not tested (not sure
this closure works with jQuery binding) and ugly as hell (don't do
it):

$(".fade").mouseover((function(x){ return function(){
     $(this).fadeTo(10,x);
}})(x));

- ricardo

On Sep 29, 11: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