Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(this) vs e.target or event.target in jquery [duplicate]

when to use $(this) and e.target?

I don't have confusion for $(this), but e.target for me is confusing because it can perform the same thing as $(this). (or I'm wrong?)

$(document).on('click',"div", function(){
        $(this).css('color', 'red');
    }
});

vs

$(document).on('click',"div", function(e){
        $(e.target).css('color', 'red');
    }
});

1 Answers

They will be the same if you clicked on the element that the event is rigged up to. However, if you click on a child and it bubbles, then this refers to the element this handler is bound to, and e.target still refers to the element where the event originated.

like image 110
Kiran Avatar answered Nov 02 '25 16:11

Kiran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!