# Control a quiz with JavaScript

> Add, open and close a quiz, and show the shopper’s quiz answers, from your theme’s own code.

Quiz · Published November 19, 2026 · https://help.brandye.com/quiz/control-a-quiz-with-javascript

When the Brandye app embed is on, every page of your theme gets a `window.Brandye` JavaScript API. Your theme developer can use it to place a quiz where the theme needs it, open or close it from custom code, and show the shopper’s answers on another page.

To open a floating quiz from a button or menu link, [use a link](https://help.brandye.com/quiz/open-a-quiz-from-a-link) instead. It needs no code.

## Before you start

1. Turn on the [Brandye app embed](https://help.brandye.com/getting-started/turn-on-the-app-embed). It loads the Brandye quiz script on every page.
2. Run your code once the script is ready:

```html
<script>
  customElements.whenDefined("brandye-quiz").then(() => {
    // window.Brandye is ready here.
  });
</script>
```

## Add a quiz to an element: `mount`

`Brandye.mount(options)` puts a quiz inside an element of your page. Calling it again for the same element updates that quiz instead of adding a second one.

| Option | What it does |
| --- | --- |
| `target` | The element, or a CSS selector for it. Required. |
| `mode` | `"embedded"` for a quiz inside the page (the default), `"popup"` for a floating widget quiz, or `"full-page"`. |
| `questionnaire` | The quiz handle. It shows under the quiz’s name in its editor, after `handle:`. |
| `placement` | A placement handle, to show one exact placement. Find it on the placement’s card in **Placement**. |
| `open` | `true` starts the quiz right away. The default is `true` for `"full-page"` and `false` otherwise. |

The quiz shows only when it has an enabled [placement](https://help.brandye.com/quiz/show-a-quiz-on-your-store) of that type that can show on this page.

```html
<div id="gift-quiz"></div>
<button type="button" onclick="window.Brandye.open('#gift-quiz')">Find my gift</button>
<script>
  customElements.whenDefined("brandye-quiz").then(() => {
    window.Brandye.mount({ target: "#gift-quiz", mode: "popup", questionnaire: "gift-quiz" });
  });
</script>
```

## Open or close a quiz: `open` and `close`

`Brandye.open(target)` and `Brandye.close(target)` take the element you passed to `mount`, its selector, or a `<brandye-quiz>` element on the page.

- On a floating widget quiz, `open` opens the popup and `close` closes it.
- On an embedded quiz, `open` starts the quiz and `close` shows its introduction again.

If no quiz is mounted on the target, both throw an error. Call `mount` first.

## Show the shopper’s answers: `getQuizAnswers`

`Brandye.getQuizAnswers(quizHandle)` returns the quiz’s questions in quiz order, with the shopper’s answers from their last submission. Use it on a [redirect page](https://help.brandye.com/quiz/redirect-after-a-quiz) or any page on the same storefront domain.

Find a question by its handle and show its answer:

```html
<p>Made for <span id="skin-type"></span> skin</p>
<script>
  customElements.whenDefined("brandye-quiz").then(async () => {
    const questions = await window.Brandye.getQuizAnswers("skin-quiz");
    const skinType = questions.find((question) => question.handle === "skin-type");
    document.getElementById("skin-type").textContent = skinType?.answers[0]?.label ?? "";
  });
</script>
```

For a question with several answers, show them all with `skinType.answers.map((answer) => answer.label).join(", ")`.

| Field | What it holds |
| --- | --- |
| `id` | The question ID. |
| `handle` | The question handle, set under **Question & Answer Handles** in the quiz editor. `null` when it has none. |
| `label` | The question, in the page’s language. |
| `options` | Every choice, each with `id`, `handle` and `label`. Choices are listed even when the shopper skipped the question. |
| `answers` | The shopper’s answers, each with `value` and `label`. For a choice, `value` is the option ID. For a text answer, both hold the shopper’s text. |
| `lead` | Only on contact detail questions: the `firstName`, `lastName`, `email` and `phone` the shopper gave. A field is listed only when the question asks for it and the shopper filled it in. |

`answers` is empty until the shopper submits the quiz. It is also empty for questions they didn’t answer, and for questions the [quiz logic](https://help.brandye.com/quiz/skip-questions-based-on-answers) skipped on their path. A contact detail question has no `options` or `answers`, and its `lead` is empty until the shopper submits. An unknown quiz handle returns an empty list.

To greet the shopper by name:

```js
const contact = questions.find(({ lead }) => lead);
greeting.textContent = contact?.lead.firstName ? `Hi ${contact.lead.firstName}` : "Hi";
```

Answers expire after 24 hours. With **Redirect only**, they are available only in the browser tab where the shopper took the quiz. Set answers with `textContent`, as in the example, because text answers are typed by shoppers.

## Common troubleshooting

**window.Brandye is undefined**

Check that the Brandye app embed is on, and run your code inside `customElements.whenDefined("brandye-quiz")`.

**mount adds nothing to the page**

Check that the quiz has an enabled placement of the mode you passed, and that it can show on this page. See [Fix a quiz that does not show on your store](https://help.brandye.com/quiz/quiz-not-showing).

**Every answers list is empty**

The shopper hasn’t submitted the quiz in this browser in the last 24 hours. With **Redirect only**, check the page in the tab where they took the quiz.
