JavaScriptDOM and events

Event.target

The element on which an event actually occurred, for example the clicked button inside a list.

Returns
The element (or other object) where the event started.
On this page

Example

#
JavaScript
const answers = document.querySelector('#answers')

answers.addEventListener('click', (event) => {
  console.log('target:', event.target)
  console.log('currentTarget:', event.currentTarget)
  console.log('Selected answer', event.target.textContent)
})

answers.children[1].click()
Console
target: <li>
currentTarget: <ul id="answers">
Selected answer B

The last line simulates a click on answer B. Click a different answer in the preview to see how target changes, while currentTarget stays the same.

Definition and usage

#

The event.target property points to the element where the event started, for example the exact button or list item the user clicked. Next to it is event.currentTarget, the element the currently running handler function is attached to.

The difference matters, because most events propagate up the tree (so-called bubbling): clicking a list item also triggers the handler on the list itself, its parent, and so on up to document. That lets you attach a single function to the parent and check event.target, instead of adding a handler to every child. This technique is called event delegation, and it also works for elements added later.

event.target can be an element nested deeper than you’d expect, e.g. a <span> icon inside a button. That’s why, with delegation, you usually look for the right element with event.target.closest(selector).

Syntax

#
Syntax
event.target
event.currentTarget

More examples

#
Delegation works for new elements
JavaScript
const todo = document.querySelector('#todo')

todo.addEventListener('click', (event) => {
  if (event.target.tagName === 'LI') {
    event.target.classList.toggle('done')
    console.log(`${event.target.textContent}: ${event.target.className}`)
  }
})

const newItem = document.createElement('li')
newItem.textContent = 'Lesson 2'
todo.append(newItem)

newItem.click()
Console
Lesson 2: done
A click on a nested element
JavaScript
document.addEventListener('click', (event) => {
  console.log(event.target)
  console.log(event.target.closest('.like'))
})

document.querySelector('.icon').click()
Console
<span class="icon">
<button id="like" class="like">

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • With event delegation, use event.target.closest(selector), since the click might have landed on a nested element, e.g. an icon inside a button.
  • In a handler function, event.currentTarget always points to the element the handler is attached to, while event.target is the element actually clicked.
  • In form fields, read the typed value through event.target.value.

Related entries

#

Spotted a mistake or missing an example? Write to us.