I have this simple script that captures a button event with jQuery:
$(function() {
$('#myButton').click(function(evt) {
console.log(evt);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="myButton" type="button">
Enviar
</button>
When the variable evt
is displayed in the console, it has several properties called:
target
currentTarget
delegateTarget
relatedTarget
All of them except relatedTarget
refer to the same object, the button that raised the event. Does anyone know what the difference between them is? If they all point to the same object, why does jQuery use them?
target
: Refers to the object element on which the event has been assigned. Considering a structure like the following:and the following code:
When
clic
thespan
event is hit, it is activated because it is due to the bubble effect (more information at: Bubbling and JQuery Events - Java Architecture ), at that moment the current target of the click (thecurrentTarget
) is thespan
, but the target as such (target
) is thediv
.The
relatedTarget
acts in the same way but is available for objects that delegate its event as we see it below:the jQuery code would be:
He
delegateTarget
is hediv.box
. It is important to mention that for non-delegated elements:delegateTarget
will always be equal tocurrentTarget
.Well according to the MDN (Mozilla developer Network) It says the following:
event.target
: The DOM Element, which has triggered an event.event.currentTarget
: Always refers to the event Handler element that has been attached to aevent.target
specific, this identifies the element in which the event occurred.event.relatedTarget
: Identifies a secondary target for the event, you can see this when working with Mouse events.event.delegateTarget
: Contains the element that is in execution progress, this is used when we work with.on()
to know who invoked the event.Means