# Search widget events

{% 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: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.abtasty.com/search/getting-started/search-widget-events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
