currentTarget and target
Question:
The Event object has two properties called currentTarget
and target
. Explain the function of these properties and give one example of their usage and explain the difference between the two.
The currentTarget
is the element you actually bound the event too this will never change where as the target
is the element that was actually clicked on for example :
See the Pen currentTarget vs target by oscar (@nopity) on CodePen.
In this example we make the whole list the currentTarget which is the element that the Event was added too. With the target
we can get all the list items on click even though though they don’t have the event applied to them. This is known as event delegation and is made possible in part due to the target elements.