Can anyone please tell me the exact difference between currentTarget
and target
property in JavaScript events with example and which property is used in which scenario?
target is the element that triggered the event (e.g., the user clicked on) currenttarget is the element that the event listener is attached to.
target is the root element that raised the event. currentTarget is the element handling the event.
The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to Event.
Definition and Usage. The target event property returns the element that triggered the event. The target property gets the element on which the event originally occurred, opposed to the currentTarget property, which always refers to the element whose event listener triggered the event.
Events bubble by default. So the difference between the two is:
target
is the element that triggered the event (e.g., the user clicked on)currentTarget
is the element that the event listener is attached to.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With