When you modify a jQuery collection within an each loop, is the modified collection included or just the original?
For example
var collection = $("div");
$(collection).each(function(){
var originalDiv = $(this);
var cloneDiv = $(this).clone();
cloneDiv.insertAfter(originalDiv);
});
Are the clone divs now included in the loop or is it only the original collection?
The jQuery object in
collectionis unaffected by thecloneandinsertAftercalls you're doing in the loop. jQuery objects are snapshots (like the ones you get fromquerySelectorAll), not live collections (like the ones you get fromgetElementsByTagNameorgetElementsByClassNameor similar), and neitherclonenorinsertAfteradds it tocollection.You can easily prove this to yourself:
Even if you were using one of the methods to add the element to a jQuery object, in general, jQuery methods create new jQuery objects rather than modifying the state of the jQuery object you call them on. (I think there are exceptions to that rule, but one doesn't immediately come to mind.) For instance, you can "add" an element by calling
add, but it doesn't change the jQuery object you call it on; instead, it creates and returns a new jQuery object containing the original elements and the new one.