> From: goodieboy
>
> Actually that was a bad example sorry! What about something like this:
> 
> $('form div.options').each(){function( i ){
>   $(this).children('[EMAIL PROTECTED]').each(function(){
>     $(this).attr('name', 'option_num_' + i);
>   });
> });
> 
> How do you access i from within the inner loop? I'm guessing 
> that the execution of the inner each loop doesn't really 
> occur until the outer one has? I just can't seem to get 
> "dynamic" values into each handlers like that. I know that 
> with Prototype, there is a function(){}.bind feature, is that 
> used on "each" also?
>
> ...
> 
> Well the syntax in both examples are wrong! :) thanks to my original
> example. Fixing the syntax still gives the same results. The value set
> in the inner "each" is the last value set to "i".
> 
> Try this:
> 
> $('div').each(function( i ){
>       $(this).find('input').attr('name', 'option_num_' + i);
> });
> 
> All elements will have the same name. the i appended to the name will
> have the last/greatest value of i. I realize that these are real/
> realtime loops, their event handlers getting fired at different times,
> but I still can't find a solution?! Anymore ideas?
>
> I meant NOT real/realtime loops like a "for". -matt

I think you must be seriously misunderstanding something, but I'm not quite
sure what it is.

There are no event handlers in either of the code snippets above, no events
that get fired at different times, and nothing asynchronous.

The "each" function is just as "realtime" as a for loop. If you trace into
the code for "each", you will see that it simply runs a for loop and calls
your callback function each time through the loop. Try it: add a "debugger;"
statement before your code, load it in Firebug or any JavaScript debugger,
and trace into the each() function. It's a very simple bit of code.

Maybe it would help to post a link to a test page that demonstrates whatever
problem you're running into. Then we could see your code in context.

-Mike

Reply via email to