> For the complete documentation index, see [llms.txt](https://partner-docs.covergenius.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://partner-docs.covergenius.com/elements/guides/handling-selection.md).

# Handling Selection

Read and control the customer's selection.

Use `on(event, handler)` to receive events from the SDK.

```js
cgElement.on('change', (detail) => { /* ... */ });
```

{% hint style="info" %}
`on()` keeps one handler for each event. A new call for the same event replaces the earlier handler. This prevents duplicate handlers when your framework renders again.
{% endhint %}

#### `ready`

Fires one time, when the panel first shows. It has no payload.

#### `change`

Fires when the selection changes. This includes a customer action and a call to `selection.set()`.

| Field                | Type                 | Meaning                                                                        |
| -------------------- | -------------------- | ------------------------------------------------------------------------------ |
| `selectedOption`     | `string`             | The stable ID of the option that changed, for example `opt_in_1` or `opt_out`. |
| `selectedProductIDs` | `string[]`           | The quote IDs that are now selected. Empty when the customer declines.         |
| `declined`           | `boolean` (optional) | `true` when the customer chose "no cover". Never `true` with quote IDs.        |

When an offer allows more than one option at once, `selectedOption` is the option that the customer just toggled. `selectedProductIDs` holds every selected quote ID.

{% hint style="success" icon="check" %}
Use `selectedProductIDs` to confirm the offer. Never use `selectedOption` for this. It is an option ID, not a quote ID.
{% endhint %}

#### `error`

Fires when the SDK cannot render the panel. The SDK hides the panel.

| Field     | Type     | Meaning                                     |
| --------- | -------- | ------------------------------------------- |
| `code`    | `string` | The reason. See Errors and Troubleshooting. |
| `message` | `string` | A description for developers.               |

Show your normal checkout without the offer when this event fires.

### Selection methods

The `selection` object on the instance controls the selection from your code.

| Method                    | Effect                                                                         |
| ------------------------- | ------------------------------------------------------------------------------ |
| `selection.set(optionId)` | Chooses an option. Use an option ID such as `opt_in_1` or `opt_out`.           |
| `selection.get()`         | Returns the last selected option ID, or `null`.                                |
| `selection.getAll()`      | Returns every selected option ID, in the order selected. Returns `[]` if none. |
| `selection.reset()`       | Clears the selection and the required prompt.                                  |
| `selection.required()`    | Shows the "you must choose" prompt.                                            |

#### Require a choice

Some offers need an explicit answer. Call `selection.required()` when the customer submits your form with no selection.

```js
checkoutForm.addEventListener('submit', (event) => {
  if (cgElement.selection.getAll().length === 0) {
    event.preventDefault();
    cgElement.selection.required();
  }
});
```

{% hint style="warning" %}
Keep these rules in mind:

* `required()` works only while nothing is selected. If an option is selected, the call does nothing.
* A customer selection clears the prompt.
* `reset()` clears the prompt.
* The SDK never shows the prompt again by itself. Call `required()` on each submit.
* The prompt shows with color only. Add your own message for assistive technology.
  {% endhint %}

### Offers with several options

The `product_rules` field of the offer response sets how many options the customer can choose. If an offer allows one option, the SDK shows radio controls. If it allows more than one, the SDK shows checkboxes.

Some choices show or hide other options. For example, a decline can reveal a second group. The SDK handles these reactions. Your code only reads the `change` event.

### Example

```js
registerElements({ environment: 'production' });

const cgElement = getElement();

cgElement.on('change', ({ selectedProductIDs, declined }) => {
  state.coverIds = declined ? [] : selectedProductIDs;
});

cgElement.render(offerResponse);
```


---

# 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://partner-docs.covergenius.com/elements/guides/handling-selection.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.
