Skip to main content
The LanderLab Quiz API provides a powerful way to extend and customize your quizzes at runtime using JavaScript. This API gives you controlled access to quiz data, blocks, and events, making it easy to create dynamic and interactive quiz experiences.

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.
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.
Forward navigation (goNext, goToStep) validates the current step before proceeding. If validation fails, navigation is prevented and a warning is logged to the console. These methods are async and return a Promise.

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.
If multiple steps have the same name, validateStepByName validates the first one found.

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:
Get a specific block by its ID:
Get a block by its name property:
Get all blocks from a specific step by step ID:
Get all blocks from a specific step by step name:
When using getBlockByName(), if multiple blocks have the same name, the function returns the first match.

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.
Some blocks use different formats other than simple strings to work with values. Check the documentation for specific block types to understand their value formats.

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.
Important Notes:
  • 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
CheckboxField supports HTML in labels, while all other blocks escape HTML for security (plain text only).
I agree to the terms ');
\ // Set label for multiple choice\ const choiceBlock = llQuizApi.getBlockByID('multiple-choice-123');\ choiceBlock?.setLabel('Select your preferred option');\ });

Practical Examples

Setting Default Values on Quiz Load

Working with Multiple Blocks

Programmatic Navigation Based on Conditions

Validating Steps

Adding UTM Parameters

Blocks with Network Validation

Conditional Visibility

Dynamic Label Updates