Understanding the Quiz API
When a quiz loads, many processes happen behind the scenes—blocks are initialized and rendered, events are registered and dispatched, and integrations run. The LanderLab Quiz API hooks into these processes and provides functions to read, write, or execute data in a way the system expects. The Quiz API is part of the LanderLab Event system and is accessible through the detail property of event objects.Accessing the Quiz API
You can access the Quiz API in any LanderLab Quiz event using the following pattern:Available Quiz Events
The LanderLab Quiz API can be accessed in the following events:- ll-quiz-init – Fires when the quiz loads initially and is bound to the DOM.
- ll-quiz-submit – Fires when the quiz gets submitted.
- ll-quiz-step-view – Fires when a step is visited.
- ll-quiz-step-leave – Fires when navigating away from a step.
- ll-quiz-exit – Fires when the tab/window is closed (based on pagehide event, not entirely reliable).
- ll-quiz-button-click – Fires when a button block is clicked (DefaultButton, Continue, Previous, Submit).
- ll-quiz-input-click – Fires when a Multiple Choice option or Image Choice option is clicked.
Getting Current Step Information
Use these functions to receive information about the step the user is currently viewing.Navigation
You can navigate around your quiz programmatically by requesting navigation. Forward navigation (goNext, goToStep) validates the current step before proceeding and prevents navigation if the step is invalid.Step Validation
You can validate a step by calling these functions. Step validation is asynchronous, so keep that in mind. You get a boolean result which indicates the validity of the step.Retrieving Blocks
The Quiz API provides multiple functions to retrieve blocks from your quiz. You can get all blocks, a specific block by ID or name, or all blocks from a particular step. Get all blocks in your quiz:Working with Block Properties
Every block provides three useful properties for easier block handling:- blockID – The unique identifier of the block that you can use for methods like getBlockByID.
- blockType – The type of the block (text-field, checkbox-field, etc.).
- variableName – The optional variable name you assigned to your block (from the block.name property).
Getting and Setting Block Values
Almost all blocks containing user data support getValue() and setValue() functions that allow you to manipulate the current state of a block easily.Resetting Blocks
You can reset any block to its initial state using the reset() function. This is similar to emptying a form field.Understanding Stateless Blocks
Stateless blocks do not support getValue(), setValue(), or reset() functions. Calling these methods on stateless blocks will throw an error. Stateless block types include: Navigation buttons (PREVIOUS, CONTINUE, SUBMIT), text blocks (HEADLINE, PARAGRAPH), LOADER, ACCORDION, LINKS, DEFAULT_BUTTON, DEFAULT_IMAGE, CUSTOM_CODE, LIST, LIST_LOADER, PROGRESS, COUNTDOWN, DIVIDER, TRACKING, and CONTAINER blocks.Block Validation
You can validate a block by calling the validate() method. Block validation is asynchronous, so keep that in mind. Depending on the block type, this may trigger network validation (e.g., phone number, email, OTP). It’s important to understand that depending on the block, a validation could trigger a module usage like network validation in the phone block for example. This is also the reason the method is asynchronous.Visibility Management
Programmatically control the visibility of a block. Hidden blocks are excluded from validation and are not included in the submission payload.- Hidden blocks (visibility.get() === false) are:
- Not validated (skipped in validation loops)
- Not included in submission payload
- Hidden in DOM (display: none)
- Shown blocks (visibility.get() === true or undefined) are:
- Validated normally
- Included in payload
- Visible in DOM
- Visibility state:
- undefined = default state (block is visible, no programmatic override)
- true = explicitly shown (overrides any default)
- false = explicitly hidden (block is not visible, not validated, not in payload)
Additional Response Data
You can use the additionalResponseData object to add, remove, or manipulate additional response data that will be sent when the user submits your quiz. A common use case is to add further hidden fields to the response, like UTM parameters and such. The data will be included in the lead submission payload with blockType ‘hidden-field’. All values must be strings. Convert numbers, booleans, or other types to strings before setting them.Custom Methods
For some blocks, we provide custom methods that might come in handy.setLabel
You can programmatically manipulate the label of blocks that support labels. This works on:- Input fields: TextField, EmailField, PhoneNumberField, TextareaField, NumberField, DatePicker, BirthDateField, ZipCodeField, GoogleAddress, OtpField, SelectField, RangeSlider
- Choice blocks: MultipleChoice, ImageChoice
- Checkbox: CheckboxField
I agree to the terms
');\ // Set label for multiple choice\ const choiceBlock = llQuizApi.getBlockByID('multiple-choice-123');\ choiceBlock?.setLabel('Select your preferred option');\ });