0

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..

Community
  • 1
  • 1
ouija
  • 186
  • 1
  • 6

2 Answers2

1

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.

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • Does this method allow you to "add" more variables, meaning is it possible to combine more than two variables into one? – ouija Jun 17 '12 at 05:55
  • @ouija. You can use this: `$StartDateTime.add('#newElement, #second, #third, #fourth');` There are other ways, but what's wrong with this? – gdoron Jun 17 '12 at 06:37
  • It doesn't seem to work under IE9 or Firefox; I'm using it to set multiple variables in conjunction with jQuery .datetimepicker and two separate input fields, and it doesn't seem to work when I define them in a variable like this, but works when defining the selector(s) themselves and not with the variable... – ouija Jun 17 '12 at 21:16
  • @ouija. It should work in any browser, you're doing something wrong, can you reproduce the problem at http://jsfiddle.net? – gdoron Jun 18 '12 at 02:36
0

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);
n0nick
  • 1,067
  • 10
  • 21