You can use an anonymous function instead of the pointer to callback
, so that you can pass in i
to callback
.
function callback(i, elem)
{
$(elem).html( data.fields[i].value );
$(elem).fadeTo('slow',1);
}
for(i=0;i<data.fields.length;i++)
{
var ident='#'+data.rID+'_'+data.fields[i].field;
$(ident).fadeTo('slow',0,function() { callback(i, this); });
}
Rather than making ident
a global variable, it would be best to declare it (making it constrained to the current function's scope), and then use this
in the callback to reference that element.
If you're not using callback
anywhere else, it may make sense to just put its implementation inside of the anonymous function, rather than defining it separately and calling out to it:
for(i=0;i<data.fields.length;i++)
{
ident='#'+data.rID+'_'+data.fields[i].field;
$(ident).fadeTo('slow',0,function() {
$(ident).html( data.fields[i].value );
$(ident).fadeTo('slow',1);
});
}
The above example with the inline anonymous function doesn't work, because the reference to i
is shared between the callbacks.