Skip to main content
The Quiz API lets your own JavaScript talk to a quiz while a visitor is filling it in. You can read what they answered, change a block, jump to a step, or submit, all without touching the quiz in the builder. You can use it anywhere JavaScript runs on the page: the quiz’s own custom code, a script block, or a tag manager snippet.

Get the API

The API is handed to you by the ll-quiz-init event as event.detail.llQuizApi. Keep that object and use it whenever you need it.
Your script can run before or after the quiz loaded. If it registers late, ll-quiz-init is replayed to it automatically, so this pattern always works.
Every other quiz event carries the same llQuizApi, so you can also react to a step or an answer and act right there. See Quiz Events.

Steps and blocks by name

Every function that takes a step or a block accepts its name or its ID. Names are what you typed in the builder, so they are what you normally use. IDs are generated and look like block-3f9c….
Give every block and step a clear name in the builder. That is the only setup the API needs.
If two blocks or two steps share a name, the first one in quiz order is used.

Read the answers

getAnswers() returns every answer given so far, one entry per block. getAnswersSimple() returns the same answers as a simple object keyed by label. Hidden blocks and empty answers are left out.
These are exactly the fields and fieldsSimple the events carry, so code that handles one handles both.

Know where the visitor is

index starts at 0. isSubmitted turns true once this visit submitted the quiz.

Find blocks

Lists only contain blocks that hold a value. Headlines, paragraphs, images and the like are never listed. getBlock() additionally finds buttons (Continue, Previous, Submit, Button), so you can change their text or disable them.
getBlock() never returns undefined. If nothing matches, or the block cannot hold a value, you get a placeholder block: every call on it logs a warning and does nothing, getValue() returns "". Your code never crashes, but check the console if something has no effect.
Input fields (text, email, phone, number, textarea, date, birth date, zip code, address, OTP), choices (multiple choice, image choice, select), checkbox, range slider, upload, signature and hidden fields.Everything else is display or navigation: headline, paragraph, image, video, divider, list, loader, countdown, progress, accordion, links, custom code, tracking, and the buttons. Calling getValue(), setValue(), reset() or validate() on one of those throws an error. Containers are transparent: the blocks inside them are listed like any other block.

Read and change a block

Every block you get back is the same kind of object, so the examples below work on any block that supports the call.

Value

Setting a value through the API triggers ll-quiz-value-change with source: 'api', so your listeners can tell it apart from the visitor typing.
  • Checkbox: 'checked' or 'unchecked'
  • Multiple choice, image choice, select: the option’s value, for example 'pro'
  • Multiple choice with several selections: the values joined by the separator configured on the block
  • Everything else: the text the visitor typed

Label and placeholder

Labels are plain text, except on a checkbox where HTML is allowed for links to terms. setLabel() and getLabel() throw on blocks that have no label.

Options

Multiple choice, image choice and select blocks expose their options:
Calling getOptions() on any other block throws.

Errors and focus

Buttons

enable() and disable() work on Continue, Submit and Button blocks and throw on anything else.

Visibility

A hidden block is not validated, not sent with the lead and not shown. undefined means you never changed it, so the block follows its own settings.

Validate

Validation is asynchronous because email, phone and OTP fields are checked against a server. Hidden blocks always pass.
When a step fails, the quiz also fires ll-quiz-validation-error with every failing block and its message, marked source: 'api'. One listener can therefore handle both the visitor’s attempts and yours. Details on the events page.
goNext(), goToStep() and submit() validate the step on screen first. If it is invalid they stay put, log a warning and fire ll-quiz-validation-error. goBack() never validates. submit() does exactly what the visitor’s Submit button does: the lead is saved, ll-quiz-submit fires with the server response, the single-submit protection applies, and the Submit button’s after-submit action runs. On a step without a Submit button the quiz submits and stays where it is. The returned promise resolves once all of that finished.

Recipes

All functions at a glance

Earlier versions had one function per lookup type. They keep working exactly as before, so nothing you already wrote needs to change. New code should use the unified functions.