Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select an array of elements in jQuery

Tags:

jquery

I need to wrap up an array of elements into a jQuery object as if they were selected so that I can call various jQuery actions on them.

I'm looking for a function like foo below that accepts an array of elements and returns a jQuery object with them in it.

var elements = [element1, element2, element3];

$(foo(elements)).click(function() {
    ...
});

Can someone shed some light on this?

Thanks much.

like image 345
Tom Tucker Avatar asked Dec 16 '22 17:12

Tom Tucker


1 Answers

Just do

$(elements).click( function(){ ... });

if your elements are actual references to the DOM

demo: http://jsfiddle.net/gaby/dVKEP/

like image 146
Gabriele Petrioli Avatar answered Jan 08 '23 14:01

Gabriele Petrioli