Introduction
The Events API allows you to listen to different lifecycle events of your quiz and execute custom code when these events occur. Here’s a basic example:Events Overview
Your quiz emits the following events per lifecycle event:| Event Name | Lifecycle Event | Data Passed |
|---|---|---|
| ll-quiz-init | When the quiz loads initially and is bound to the DOM | quizId, llQuizApi |
| ll-quiz-submit | When the quiz gets submitted | quizId, stepId, stepName, fields, fieldsSimple, llQuizApi, response |
| ll-quiz-step-view | When a step is visited | quizId, stepId, stepName, fields, fieldsSimple, llQuizApi |
| ll-quiz-step-leave | When navigating away from a step | quizId, stepName, llQuizApi |
| ll-quiz-exit | When the tab/window in which the quiz lives gets closed | quizId, llQuizApi |
| ll-quiz-button-click | When a button block is clicked (DefaultButton, Continue, Previous) | quizId, blockId, blockName, stepId, stepName, llQuizApi |
| ll-quiz-input-click | When a Multiple Choice or Image Choice option is clicked | quizId, blockId, blockName, stepId, stepName, optionId, optionValue, optionLabel, llQuizApi |
ll-quiz-init Event
When: When the quiz loads initially and is bound to the DOM. Event Detail:- quizId (string): The unique identifier of the quiz
- llQuizApi (LlQuizApi): The LlQuiz API instance for interacting with the quiz
ll-quiz-submit Event
When: When the quiz gets submitted (typically when the submit button is clicked and the form is successfully submitted). Event Detail:- quizId (string): The unique identifier of the quiz
- stepId (string): The step ID where submission occurred
- stepName (string): The step name where submission occurred
- fields (Field[]): Array of field objects with detailed information (see Fields Structure below)
- fieldsSimple (FieldsSimple): Key-value object with label as key and value as string (see FieldsSimple Structure below)
- llQuizApi (LlQuizApi): The LlQuiz API instance for interacting with the quiz
- response (any): The API response from the submission endpoint. Can be:
- The parsed JSON response if the API returns JSON
- The Response object if it’s not JSON
- null if the API call failed
ll-quiz-step-view Event
When: When a step is visited (whenever the user navigates to a new step). Event Detail:- quizId (string): The unique identifier of the quiz
- stepId (string): The step ID that was viewed
- stepName (string): The step name that was viewed
- fields (Field[]): Array of field objects from the current step (see Fields Structure below)
- fieldsSimple (FieldsSimple): Key-value object with label as key and value as string for the current step (see FieldsSimple Structure below)
- llQuizApi (LlQuizApi): The LlQuiz API instance for interacting with the quiz
ll-quiz-step-leave Event
When: When navigating away from a step (before the next step is shown). Event Detail:- quizId (string): The unique identifier of the quiz
- stepName (string): The step name being left
- llQuizApi (LlQuizApi): The LlQuiz API instance for interacting with the quiz
ll-quiz-exit Event
When: When the tab/window in which the quiz lives gets closed. This event is based on the native pagehide event, which, unfortunately, is not entirely reliable. The event may not fire in all scenarios (e.g., force quit, browser crash, or certain mobile browser behaviors). Don’t rely on it for critical operations. Event Detail:- quizId (string): The unique identifier of the quiz
- llQuizApi (LlQuizApi): The LlQuiz API instance for interacting with the quiz
ll-quiz-button-click Event
When: When a button block is clicked. This includes DefaultButton, Continue, Previous, and Submit button blocks. Event Detail:- quizId (string): The unique identifier of the quiz
- blockId (string): The unique identifier of the button block that was clicked
- blockName (string): The name property of the button block
- stepId (string): The step ID where the button is located
- stepName (string): The step name where the button is located
- llQuizApi (LlQuizApi): The LlQuiz API instance for interacting with the quiz
ll-quiz-input-click Event
When: When a Multiple Choice option or Image Choice option is clicked. Event Detail:- quizId (string): The unique identifier of the quiz
- blockId (string): The unique identifier of the block where the option was clicked
- blockName (string): The name property of the block
- stepId (string): The step ID where the block is located
- stepName (string): The step name where the block is located
- optionId (string): The unique identifier of the clicked option
- optionValue (string): The value of the clicked option
- optionLabel (string): The label of the clicked option
- llQuizApi (LlQuizApi): The LlQuiz API instance for interacting with the quiz
Understanding fields and fieldsSimple
fields and fieldsSimple contain all information from your quiz in different structures. Quick Overview:- fieldsSimple: A key-value object where keys are field labels and values are user inputs as strings
- fields: An array of detailed field objects with complete metadata (id, label, value, key, stepId, stepName)
Using LlQuiz API in Events
All events provide access to the llQuizApi instance, which allows you to interact with your quiz programmatically. Available Methods:- llQuizApi.getCurrentStepID() – Returns the current step ID (e.g., “step-1”)
- llQuizApi.getCurrentStepName() – Returns the current step name (e.g., “start”)
- llQuizApi.getBlocks() – Returns all blocks in the quiz
- llQuizApi.getBlockByID(blockId) – Returns a specific block by its ID
- llQuizApi.getBlockByName(blockName) – Returns a block by its name property (first match)
- llQuizApi.getBlocksByStepID(stepId) – Returns all blocks in a specific step
- llQuizApi.getBlocksByStepName(stepName) – Returns all blocks in a specific step
- block.getValue() – Get the current value of the block
- block.setValue(value) – Set the value of the block
- block.reset() – Reset the block to its initial state