So I tried this:
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);
});
She doesn’t work in jsFiddle.
What I need is to keep a collection of some <LI> 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.
i know i can get around it by something like:
function additems(items){
items.each(function(){ allItems.pushStack(this);});
}
or by just sending them as a list of HTML <LI> Elements, but I’d rather do it something like the above and I haven’t found a clean and efficient way of doing this.
Thanks!
PS: I’d rather not need a plugin.
UPDATE
let me explain more. I have one function that does something and it has an <ul> element from which i can pull out the <li> elements.
I then want to send these elements to another function to keep them for later.
function doSomething1()
{
//Do something
var ulElement = getFromSomewhere();
additems(jQuery(ulElement).find("li"));
// do something else
return;
}
function additems(items)
{
MyObject.allItems.pushStack(items)
}
the jsFiddle was Simplified compared to my code.
new jsfiddle with “#” fixed http://jsfiddle.net/LPkkT/8/
ANSWER
Upon adding Quincy’s answer to my fiddle in a way that didn’t remove all my code i got:
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);
});
And this works.
Firstly, if you want to select by id, you have to add a
'#'in the selector.And you can add jquery collections using the
add()methodhttp://jsfiddle.net/LPkkT/4/