ouija
ouija

Reputation: 196

Combine jQuery object with new selector

How would you do a similar function as rsplak's answer here https://stackoverflow.com/a/7079032/1330629 but include a defined variable in this? For example, I have a variable defined so that I'm not querying the DOM repeatedly, such as: var $StartDateTime = $("#StartDateTime");

and would like to include it with another selector, which I haven't defined as a variable.

Is this possible? I'm thinking along the lines of:

$($StartDateTime,link,#upload_link2,#upload_link3").each(function(){
    $(this).upload({
        //whateveryouwant
    });
});

But obviously that's not going to work..

Upvotes: 0

Views: 186

Answers (2)

n0nick
n0nick

Reputation: 1145

Either use add as @gdoron suggested

var $combined = $StartDateTime.add('#newElement');

Or (IMHO producing a more elegant code) jQuery.merge:

var $newElement = $('#newElement');
var $combined = $.merge($StartDateTime, $newElement);

Upvotes: 0

gdoron
gdoron

Reputation: 150253

You should use add:

var $combined = $('#newElement').add($StartDateTime);

Or this overload:

var $combined = $StartDateTime.add('#newElement');

add docs:

Description: Add elements to the set of matched elements.

Upvotes: 1

Related Questions