3 ms·
> Also, in your first example, you don't have to rewrap containers[index] in a jQuery object. All the elements in the selection are already wrapped. Really? I
by emehrkay 16y ago
> Also, in your first example, you don't have to rewrap containers[index] in a jQuery object. All the elements in the selection are already wrapped.
Really? I think that is the whole point of this long comment thread -- that the elements in a collection arent already wrapped.
http://jsfiddle.net/8nqtA/ http://jsfiddle.net/8nqtA/
This doesnt work unless i wrap a[i] in $()
But like you're saying, it may be fruitless since the dom is the bottleneck and we're bypassing it by already having the element.
- M1573RMU74710N 16y agoWell 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
- xtian 16y agoAh yeah, I forgot you had to use .eq().