Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind jQuery events on multiple PREDECLARED jQuery objects

This is how to bind multiple events on a couple jQuery selectors:

$('#selector1,.selector2').bind('event', function (e, ui) {
   // Stuff
});

But! How do you bind on predeclared jQuery objects. Example:

var jSelector1 = $('#selector1');
var jSelector2 = $('.selector2');

Because, the following doesn't work:

jSelector1,jSelector2.bind(...);

nor does:

$jSelector1,$jSelector2.bind(...);
($jSelector1,$jSelector2).bind(...);
(jSelector1,jSelector2).bind(...);
like image 659
Kyle Cureau Avatar asked Aug 08 '10 21:08

Kyle Cureau


1 Answers

This should work, assuming your variables hold jQuery objects

$.each([jSelector1, jSelector2], function(i,v) {

    v.bind( ... );

});
like image 157
Russ Cam Avatar answered Oct 22 '22 14:10

Russ Cam