Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery multiple selectors, get which selector triggered event

When handling events with multiple selectors, such as:

$('.item a, .another-item a').click(function(e) {

});

Is it possible to determine which parent selector triggered the event? Was it .item or .another-item ?

Thank you!

like image 411
dzm Avatar asked Dec 05 '22 14:12

dzm


2 Answers

Since a selector can be just about anything, you'd have to check for specific, such as:

if($(this).is('.item a')){
  //your code here
} else if ($(this).is('.another-item a')){
  //more here
}
like image 76
Adriano Carneiro Avatar answered Dec 28 '22 09:12

Adriano Carneiro


You can use jQuery is.

if($(this).is('.item a')){
      // code
   }
   else if($(this).is('.another-item a')){//remove 'if ' in case there are only two selector separated by commas.
        //code..

   }
like image 26
Anoop Avatar answered Dec 28 '22 11:12

Anoop