Know where click comes from when you have multiple options

Asked

Viewed 36 times

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?

1 answer

4


Inside this Event Handler you can use the this. The this is the delegated element. To find out if a given element has a class you can use the el.classList.

Take a look here (http://jsfiddle.net/sze95py7/), 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 can do

if(this.classList.contains('a')){
    // fazer algo
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.