Know where the click comes from when you have multiple options

3

In this case:

$('body').on('click', '.a, .b, .c', function(e) {
...

How will I know when the click came from .a or .c for example?

    
asked by anonymous 19.09.2015 / 17:02

1 answer

4

Within this event handler you can use this . this is the delegated element. To know if a given element has a class you can use el.classList .

Take a look here ( link ), this example:

$('body').on('click', '.a, .b, .c', function (e) {
    console.log(this, this.classList);
});

So to know if it's the element with the class a you can do

if(this.classList.contains('a')){
    // fazer algo
}
    
19.09.2015 / 17:36