Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat method do use when adding a jquery collection to a jQuery collection?
    text
    copied!<p>So I tried this:</p> <pre><code>var allItems = jQuery(); function additems(items) { allItems = allItems.pushStack(items); } additems(jQuery("#ul1").find("li").first()); additems(jQuery("#ul2").find("li").first()); additems(jQuery("#ul3").find("li").first()); allItems.each(function() { jQuery("ul1").append(this); }); </code></pre> <p>She doesn't work in <a href="http://jsfiddle.net/LPkkT/" rel="nofollow">jsFiddle</a>. </p> <p>What I <strong>need</strong> is to keep a collection of some <code>&lt;LI&gt;</code> items hopefully in a jQuery object. And i'd like it to use a function passing in a jQuery object. I also need to add to the collection inside a function like what I have above so it can be done at different times in the code.</p> <p>i know i can get around it by something like:</p> <pre><code>function additems(items){ items.each(function(){ allItems.pushStack(this);}); } </code></pre> <p>or by just sending them as a list of HTML <code>&lt;LI&gt;</code> Elements, but I'd rather do it something like the above and I haven't found a clean and efficient way of doing this. </p> <p>Thanks!</p> <p><em>PS: I'd rather not need a plugin.</em> </p> <p><strong>UPDATE</strong></p> <p>let me explain more. I have one function that does something and it has an <code>&lt;ul&gt;</code> element from which i can pull out the <code>&lt;li&gt;</code> elements.</p> <p>I then want to send these elements to another function to keep them for later.</p> <pre><code>function doSomething1() { //Do something var ulElement = getFromSomewhere(); additems(jQuery(ulElement).find("li")); // do something else return; } function additems(items) { MyObject.allItems.pushStack(items) } </code></pre> <p>the jsFiddle was Simplified compared to my code.</p> <p>new jsfiddle with "#" fixed <a href="http://jsfiddle.net/LPkkT/8/" rel="nofollow">http://jsfiddle.net/LPkkT/8/</a></p> <p><strong>ANSWER</strong> </p> <p>Upon adding Quincy's answer to my fiddle in a way that didn't remove all my code i got:</p> <pre><code>var allItems = jQuery(); function additems(items) { allItems = allItems.add(items); } additems(jQuery("#ul1").find("li").first()); additems(jQuery("#ul2").find("li").first()); additems(jQuery("#ul3").find("li").first()); allItems.each(function() { jQuery("ol").append(this); }); </code></pre> <p>And this works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload