Your code looks like it'll work, it could even be reduced to: $('form div.options').each(){function( i ){ $(this).children('[EMAIL PROTECTED]').attr('name', 'option_num_' + i); });
--John On 8/23/07, goodieboy <[EMAIL PROTECTED]> wrote: > > Hi John, > > 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? > > Thanks, > Matt > > On Aug 23, 12:16 am, "John Resig" <[EMAIL PROTECTED]> wrote: > > Like so: > > > > $('input').each(function(i){ > > $(this).attr('name', i); > > }); > > > > Nice and simple! > > > > --John > > > > On 8/23/07, goodieboy <[EMAIL PROTECTED]> wrote: > > > > > > > > > OK, I thought I knew jQuery pretty well. But this is completely > > > stumping me. What I want to do is iterate through a newly attached set > > > of input elements, and set their names based on a dynamic > > > (incrementing) number. But the only value that ever gets used is the > > > initial value of the variable. I've tried everything, attaching the > > > value to the dom object, and then in the loop trying to access it > > > (still the same original value), using a global variable, a function > > > to get the value, all return the original value that I set the > > > variable to. > > > > > What am I doing wrong? Here is an example: > > > > > for(i=0; i<10; i++){ > > > $('input').each(function(){ > > > $(this).attr('name', i); > > > }); > > > } > > > > > Here, all of the inputs get the value 9. How can I make then all get > > > the current incremented value of i? > > > > > Thank you for any tips! > > > Matt > >