Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Event.target and Event.type

`event.target` identifies the event’s target, while `event.type` gives its name. `event.currentTarget` identifies the object on which the running listener was registered.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

event.target
event.type

Event.target/type is shorthand for two read-only instance properties, event.target and event.type, not a JavaScript expression to write. They are web platform APIs, not part of ECMAScript 2024. Availability depends on the runtime.

event.target gives the event’s target as seen by the listener. It is an EventTarget, not necessarily an Element; a newly constructed event has a target of null before its first dispatch. Check that the target is an element before calling an element-specific method such as closest().

event.currentTarget is the object on which the running listener was registered. When an event bubbles from a child, target can be the child while currentTarget is the parent. That distinction makes event delegation possible. currentTarget is null outside the listener invocation, so save its value before await if you need it later. Across a shadow DOM boundary, the target seen by an outside listener can be retargeted to the shadow host.

event.type is a case-sensitive string. Use addEventListener("click", handler), not "onclick": onclick is the name of a handler property. A script can also create an event of type "click", so the type alone does not prove that a user clicked.

Examples

{
  // Node.js has EventTarget but no DOM tree. Model only the bubbling used here.
  const dispatchState = new WeakMap();

  class DemoEvent extends Event {
    constructor(type, options) {
      super(type, options);
      dispatchState.set(this, { target: null, currentTarget: null });
    }
    get target() { return dispatchState.get(this).target; }
    get currentTarget() { return dispatchState.get(this).currentTarget; }
  }

  class DemoNode extends EventTarget {
    #parent;

    constructor(parent = null) {
      super();
      this.#parent = parent;
    }

    dispatchEvent(event) {
      const state = dispatchState.get(event);
      state.target = this;
      try {
        for (let node = this; node !== null; node = event.bubbles ? node.#parent : null) {
          state.currentTarget = node;
          EventTarget.prototype.dispatchEvent.call(node, event);
        }
      } finally {
        state.currentTarget = null;
      }
      return !event.defaultPrevented;
    }
  }

  const hasDOM = typeof document !== "undefined";
  const parent = hasDOM ? document.createElement("div") : new DemoNode();
  const child = hasDOM
    ? parent.appendChild(document.createElement("button"))
    : new DemoNode(parent);
  const EventClass = hasDOM ? Event : DemoEvent;

  const event = new EventClass("demo", { bubbles: true });
  console.log(`before: type=${event.type}, target=${event.target}`);

  parent.addEventListener("demo", (e) => {
    console.log(`listener: type=${e.type}, targetIsChild=${e.target === child}, currentTargetIsParent=${e.currentTarget === parent}`);
  });

  child.dispatchEvent(event);
  console.log(`after: currentTarget=${event.currentTarget}`);
}

Result

before: type=demo, target=null
listener: type=demo, targetIsChild=true, currentTargetIsParent=true
after: currentTarget=null

Paste the block into a browser developer console. No click is needed: `dispatchEvent()` invokes the parent listener synchronously because `bubbles: true` lets the event dispatched to the child bubble up. You can also run the block as a `.js` file in Node.js 24, where the example uses a simplified model of bubbling without a DOM.

Related lessons

  • JavaScript · Lesson 30 · Advanced · Version: 4

Sources