Get the API
The API is handed to you by thell-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.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 likeblock-3f9c….
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.
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.
Which blocks hold a value?
Which blocks hold a value?
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
ll-quiz-value-change with source: 'api', so your listeners can tell it apart from the visitor typing.
Value formats by block
Value formats by block
- 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
setLabel() and getLabel() throw on blocks that have no label.
Options
Multiple choice, image choice and select blocks expose their options: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
undefined means you never changed it, so the block follows its own settings.
Validate
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.Navigate and submit
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
Prefill from the URL
Prefill from the URL
Show a custom progress text
Show a custom progress text
Jump to the first field with an error
Jump to the first field with an error
Show a block only for one answer
Show a block only for one answer
Send the answers to your own endpoint
Send the answers to your own endpoint
All functions at a glance
Older function names (still supported)
Older function names (still supported)
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.