I want to bind multiple elements that are defined as a variable. I know this is possible:
$('selector1, selector2').bind(...)
But I want to do something like this:
$($element1, $element2, $element3).bind(...)
Any ideas?
You have to add them to one result set:
$element1.add($element2).add($element3).bind(...)
Or for an arbitrary number of elements:
var elems = [$element1, $element2, $element3, ...];
var $result = $();
$.each(elems, function() {
$result = $result.add(this);
});
$result.bind(...);
Or with Array.reduce:
[$element1, $element2, $element3, ...].reduce(function($result, $elem) {
return $result.add($elem);
}, $()).bind(...);
You can use add();
var $all = $element1.add($element2).add($element3).bind( ... ); //.add(...)
0 in the get methods, that's why it didn't work jsfiddle.net/Uj9hk/3
$($element1.selector + ", " + $element2.selector + ", " + $element3.selector)