> Markdown version of [Element Events](https://vaadin.com/docs/next/flow/component-internals/element-api/event-listener). Section index: [llms.txt](https://vaadin.com/docs/next/flow/llms.txt)

# Listening to Element Events

The Element API contains methods to receive events and event data from the browser and handle them on the server side.

## <a id="registering-event-listeners"></a>Registering Event Listeners

Use the `Element.addEventListener()` method to listen to any browser event.

Example 1. Using the `addEventListener()` method to react to a `click` event

```java
Element helloButton = ElementFactory.createButton("Say hello");
helloButton.addEventListener("click", e -> { // (1)
    Element response = ElementFactory.createDiv("Hello!");
    getElement().appendChild(response); // (2)
});
getElement().appendChild(helloButton);
```

1. Clicking the "Say hello" button in the browser sends the event to the server for processing.

2. A new `<div>Hello!</div>` element is added to the DOM.

## <a id="accessing-data-from-events"></a>Accessing Data from Events

You can get more information about the element or user interaction by defining the required event data on the `DomListenerRegistration` returned by the `addEventListener()` method.

For a value that’s a property of the event or of the element, use `addEventDataPath()` (V25.4 pre-release). The path starts with `event`, for the fired event, or `element`, for the element the listener is added to, followed by property names that each start with `.` or `?.`.

Example 2. Using the `addEventDataPath()` method to define the required event data

```java
helloButton.addEventListener("click", this::handleClick)
    .addEventDataPath("event.shiftKey") // (1)
    .addEventDataPath("element.offsetWidth");

private void handleClick(DomEvent event) {
    JsonNode eventData = event.getEventData(); // (2)
    boolean shiftKey = eventData
            .get("event.shiftKey").asBoolean(); // (3)
    double width = eventData
            .get("element.offsetWidth").asDouble();

    String text = "Shift " + (shiftKey ? "down" : "up");
    text += " on button whose width is " + width + "px";

    Element response = ElementFactory.createDiv(text);
    getElement().appendChild(response);
}
```

1. The requested event data values are sent as a JSON object from the client.

2. You can retrieve the event data using the `DomEvent.getEventData()` method in the listener. It returns a Jackson `JsonNode` (from `tools.jackson.databind`).

3. Look up each value by the same path you passed to `addEventDataPath()`, then read it with the accessor that matches its type — for example `asBoolean()`, `asInt()`, `asDouble()`, or `asText()`.

> **Note: Return Type Changed from Elemental JSON**
>
> `DomEvent.getEventData()` now returns a Jackson `JsonNode` (`tools.jackson.databind.JsonNode`) instead of the previous Elemental `JsonObject`. Code written for earlier versions no longer compiles: replace `JsonObject` with `JsonNode`, and the Elemental getters such as `getString(key)`, `getBoolean(key)`, and `getNumber(key)` with `get(key).asText()`, `get(key).asBoolean()`, and `get(key).asDouble()`. See [the upgrade guide](https://vaadin.com/docs/next/upgrading.md) for the full list of Elemental-to-Jackson changes.

To map a DOM node to its server-side `Element`, use `addEventDataElementPath()` (undefined pre-release) with a path such as `event.relatedTarget`, and read the element with `DomEvent.getEventDataElement()`.

### <a id="declaring-event-data"></a>Declaring Event Data (V25.4 pre-release)

For any other value, declare a method that returns `DomEventData` in a `@JsDefinition` interface that extends `JsSnippets`, and pass what the method returns to `addEventData()`. The `@JsExpression` of the method is the body of a function that returns the value to send. Unlike other snippets, whose function takes no parameters unless the method is annotated with `@JsFunctionParameterNames`, this function always gets the DOM `event` and the `element` the listener is added to.

Example 3. Declaring event data and reading it with `getEventData(DomEventData)`

```java
@JsDefinition
public interface ClipboardJs extends JsSnippets {

    @JsExpression("return event.clipboardData?.getData($0) || null;") // (1)
    DomEventData<String> clipboardData(String mimeType);
}

private static final ClipboardJs CLIPBOARD = JsSnippets.of(ClipboardJs.class);

private void addPasteListener(Element field) {
    DomEventData<String> pasted = CLIPBOARD.clipboardData("text/plain");
    field.addEventListener("paste", event -> {
        String text = event.getEventData(pasted); // (2)
        // ...
    }).addEventData(pasted);
}
```

1. The arguments of the method call are available to the expression as `$0`, `$1`, and so on.

2. The value has the type the method declares. Pass the same event data that was added to the listener. When all arguments are plain values, such as strings and numbers, one created with the same method and equal arguments also works.

To get a DOM node that isn’t at a property path, such as `event.composedPath()[0]`, declare it as `DomEventData<Element>`. The server receives the closest server-side `Element`, which you read with `DomEvent.getEventDataElement()`.

The build adds the declared expression to the frontend bundle, so the browser doesn’t compile anything from a string. See [Declaring Snippets](https://vaadin.com/docs/next/flow/component-internals/element-api/calling-javascript.md#declaring-snippets) for more about `JsSnippets` interfaces.

## <a id="filtering-and-debouncing"></a>Filtering and Debouncing

Filtering and debouncing are covered in detail in [Declaring and Firing Component Events](https://vaadin.com/docs/next/flow/component-internals/events.md#limiting-event-frequency), in the context of the `@DomEvent` annotation. However, you can configure the exact same settings through the `DomListenerRegistration` object.

### <a id="declaring-a-filter"></a>Declaring a Filter (V25.4 pre-release)

To filter events, declare the filter as a method that returns `DomEventFilter` in a `JsSnippets` interface, and pass what the method returns to `setFilterExpression()`. Like declared event data, the expression gets the DOM `event` and the `element` the listener is added to, and returns `true` for the events to send to the server.

Example 4. Declaring a filter and using it with `setFilterExpression()`

```java
@JsDefinition
public interface KeyFiltersJs extends JsSnippets {

    @JsExpression("return event.key === $0;") // (1)
    DomEventFilter isKey(String key);
}

private static final KeyFiltersJs FILTERS = JsSnippets.of(KeyFiltersJs.class);

private void addSubmitOnEnter(Element field) {
    field.addEventListener("keydown", event -> submit())
            .setFilterExpression(FILTERS.isKey("Enter"));
}
```

1. As with event data, the arguments of the method call are available as `$0`, `$1`, and so on.

`465CAE8B-8B0D-426B-818F-1CF58A9BD034`
