3 ms·
Well there's a few problems with that example: 1) You've got a "global" named a and then a local variable (via the argument) inside the each function called "a
by M1573RMU74710N 16y ago
Well there's a few problems with that example:
1) You've got a "global" named a and then a local variable (via the argument) inside the each function called "a"...so that really confuses things.
2) a[i] where a is a jQuery object returns an unwrapped DOM element...what you want is .index().
3) What you're doing is sort of roundabout...
Why not:
var d = $('div'),
a = $('a');
a.each(function(i, el){
d.text(d.text() +' '+ $(this).text() );
});
Or if you really want to do it your way:
var d = $('div'),
a = $('a');
a.each(function(i, el){
d.text(d.text() +' '+ a.index(i).text() );
});
- arethuza 16y agoShouldn't the second one be: var d = $('div'), a = $('a'); a.each(function(i, el){ d.text(d.text() +' '+ a.eq(i).text() ); }); Using eq(i) rather than index(i)?
- emehrkay 16y agoI feel like this should have been the first replay to my rant. The nodes are already wrapped in the jQuery object, just need to use eq to access them by index. I've never seen that before, thanks