javascript - jQuery: How to join sets of selected elements? -


i must missing because i've looked around , can't find simple answer this.

i have many jquery objects i'm constructing programmatically:

var jq1 = $('#stuff'); var jq2 = $('p.error'); var jq3 = $('<span>hey!</span>'); /* , on */ 

question: how create jquery object concatenation of these objects?

i know can use .add() method add single jquery selection, in loop , add these elements 1 one, creating new object each iteration. doesn't jquery have way join of these without creating bunch of new objects?


update: tried keep generic, give more context, want generate unknown number of elements in third line. generated elements of unknown type. inject them in dom, of know nothing about, , still need have jquery object containing elements manipulate them later.

you can construct jquery object using array:

var jq1 = $('#stuff'); var jq2 = $('p.error'); var jq3 = $('<span>hey!</span>');  var $all = $([jq1[0], jq2[0], jq3[0]]); 

or, if have lot of elements, , don't want mess array indexing:

var $all = $([jq1, jq2, jq3].map(function(elt){return elt[0];})); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -