views:

30

answers:

1

Why doesn't this work in jQuery 1.4.2?


var $list = $([]);
for(var i=0; i<50; i++) {
    $list.add( $('<div/>', { id: 'jake', class: 'test' }).data('test', { hi: 'hello' }) );
}
alert($list.size()); // 0

Thanks!

A: 

Why add doesn't work I don't know, but you can replace it with push due to jQuery being an Array-like object, which should do what you want.

x1a4
awesome, i thought i tried that too, but apparently not! thanks.
taber
it looks like in order to use .clone(true) on $list, my $list array needs to be a dom node (eg: $('<div />') but i don't want a containing div, i just want a list of dom nodes with no parent. (like an array!) is that possible?
taber
it's not the end of the world if i have to have a containing div i guess. it's just extra junk in the dom. oh well.
taber
In your case, because each div is created separately before being pushed onto $list, each element in $list is actually its own separate jQuery object, so THOSE are what you need to call clone on:`clones = $list.map(function () { return this.clone(true); });` and you should be good to go.
x1a4
and no, you definitely won't need the containing div :)
x1a4
cool... thanks!
taber