Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .delegate() on multiple elements

When using the .delegate can I choose multiple elements or should I have to use .delegate for every element that I need to work with? (function will be the same for all elements)

e.g.

$('#div').delegate('a', 'click' (function(){ // This is ok
         .delegate('a, element_2, element_3', 'click' (function(){ // IS THIS OK??
like image 450
Pavlos1316 Avatar asked Jul 22 '11 14:07

Pavlos1316


3 Answers

That is fine since 'a, element_2, element_3' is a valid selector, which is the first argument of .delegate().

BUT, your arguments are not separated by commas properly, it should be:

$('#div').delegate('a, element_2, element_3', 'click', function(){
    //function go here
});
like image 198
StuperUser Avatar answered Nov 07 '22 15:11

StuperUser


That second method appears to be okay as it a, element_1, element_2 returns a set of elements that will have the .delegate() function applied.

like image 39
Phil Avatar answered Nov 07 '22 15:11

Phil


Yes it is OK, the documentation states

.delegate( selector, events )

Selector being any valid jQuery selector. even your first call may return more than one element.

like image 1
Andrew Avatar answered Nov 07 '22 13:11

Andrew