> For the complete documentation index, see [llms.txt](https://docs.abtasty.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.abtasty.com/commerce/search/search-widget-events.md).

# Search widget events

This feature allows you to listen to events dispatched by the search widget when users perform searches, giving you access to search results and product data outside the widget.

{% stepper %}
{% step %}

### Search event

When users perform searches, there are cases where you'll need access to the returned products (or product IDs) outside the widget.<br>

**Custom event:**

```javascript
const searchEvent = new CustomEvent('abtasty_search', {
    detail: {
        results,
        widgetId, // current widget unique ID
    }
});
```

**Listen to the event:**

```javascript
window.addEventListener('abtasty_search', (event) => {
    const { results, widgetId } = event.detail;
    console.log(widgetId, results);
});
```

{% endstep %}

{% step %}

### Search result event

An event is triggered for each product when it's added to the search results list. Use this to perform actions on individual products.

**Custom event:**

```javascript
const productEvent = new CustomEvent('abtasty_search_result', {
    detail: {
        resultElement: resultHTMLElement,
        index,    // index of the element in the hits array
        widgetId, // current widget unique ID
    }
});
```

**Listen to the event:**

```javascript
window.addEventListener('abtasty_search_result', (event) => {
    const { widgetId, index, resultElement } = event.detail;
    console.log(widgetId, index, resultElement);
});
```

{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.abtasty.com/commerce/search/search-widget-events.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
