Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript event.currentTarget vs this

Is there a difference between event.currentTarget and this? What about performance?

like image 292
Hussein Avatar asked Feb 26 '11 08:02

Hussein


2 Answers

jQuery documentation clearly says, that event.currentTarget is equal to this in most cases (unless you changing scope manually with jQuery.proxy or similar):

This property will typically be equal to the this of the function.

If you are using jQuery.proxy or another form of scope manipulation, this will be equal to whatever context you have provided, not event.currentTarget

https://api.jquery.com/event.currentTarget/

like image 106
Anton Belonovich Avatar answered Sep 27 '22 16:09

Anton Belonovich


The currentTarget event attribute returns the element whose event listeners triggered the event. This is only particularly useful during capturing and bubbling.

You can also use this keyword, but when you use the Microsoft event registration model the this keyword doesn’t refer to the HTML element.

Please see following link for more information: http://www.quirksmode.org/js/events_order.html

Problems of the Microsoft model

But when you use the Microsoft event registration model the this keyword doesn’t refer to the HTML element. Combined with the lack of a currentTarget–like property in the Microsoft model, this means that if you do

element1.attachEvent('onclick',doSomething)
element2.attachEvent('onclick',doSomething)

you cannot know which HTML element currently handles the event. This is the most serious problem with the Microsoft event registration model and for me it’s reason enough never to use it, not even in IE/Win only applications.

Note:: it may be,now resolved it

like image 37
Manish Trivedi Avatar answered Sep 27 '22 17:09

Manish Trivedi