Can I update a jQuery collection of objects?

255 Views Asked by At

I have inited many elements, which need to animate, so I just want to init them once. I said this up front to discourage you from answering to re-define it, because I'd have to re-define all the events for all the elements in mid-animation.

I have a ul with tens of li, but when I detach one of the Li, the next animation STILL happens on the whole collection of elements that existed when inited. Not on the CURRENT collection.

So I need some way to "update" the jquery object (collection of LIs) to remove the ones that don't exist anymore.

Is there anything provided for that?

I don't know if each collection knows what it was made from for even call it an "update" so for now I will just test if the parent is still the same and the element is not empty.

Thanks.

2

There are 2 best solutions below

2
On

A code sample might help us a lot.

But what i understood from your question is that you have a jQuery collection of objects say '$listCollection' from which you remove an li element say 'listElement'(DOM object, not jQuery object), then this is what you want

$listCollection.splice($listCollection.indexOf(listElement),1);

Note: The above code will behave buggy if 'listElement' is not in '$listCollection'

0
On

Ok, so in short, what I expected, is that it seems there is nothing provided by jQuery.

I had to make the collection variable global, so the functions that depend on that collection later can see the updated version, rather than passing it (the collection variable) to the events functions which won't be aware of future changes.