tags:

views:

39

answers:

4

Hi,

I would select multiple DOM elements using a typical code such as this:

$('#ele1, #ele2, #ele3').click(function () {});

If I have variables of each DOM element, e.g.

var domEle1 = $('#ele1');
var domEle2 = $('#ele2');
var domEle3 = $('#ele3');

How can I select them all at once? Instead of individually...

domEle1.click(function () {});

Thanks!

+1  A: 

You can use an array for this:

$( [ '#ele1', '#ele2', '#ele3' ] ).click( function() {
   //...
} );
Jacob Relkin
+2  A: 
jQuery(
    [domEle1, domEle2, domEle3]
    )
David Dorward
+1  A: 

It's like that:

$(domEle1).add(domEle2).add(domEle3).hide(); //for example
Zlatev
A: 
$([domEle1, domEle2, domEle3]).each( function() {
    $(this).click(function(){alert('hello')});
})
Reigel