Search code examples
jqueryjquery-chainingjquery-append

jQuery adding / chaining elements to one variable


how can I turn this into something faster:

$.each(data, function(key, val) 
{ 
    rcItemsLi.eq(index++).append('<div class="rc-item-content clear hidden"><p class="rc-item-context">' + val[0] + '</p>' + val[1] + '</div>'); 
}); 
rcItemsContent = $('.rc-item-content');

in this example, I first append the elements to where I want, then I use .rc-item-content selector to "find" all the elements and store them in rcItemsContent variable.


for example:

$.each(data, function(key, val) 
{ 
    rcItemsContent.add($('<div class="rc-item-content clear hidden"><p class="rc-item-context">' + val[0] + '</p>' + val[1] + '</div>').appendTo(rcItemsLi.eq(index++))); 
});

in this example, what I'm trying to achieve (which of course I don't), is to add / chain the element in the variable and append it to where I want at the same time.


Solution

  • .add creates a new collection.

    rcItemsContent = rcItemsContent.add(...