top of page

Create a quiz on the Wix website.

Jan 19, 2022
4 min read

Updated: 17 hours ago

Today we will look at how to create a quiz on a Wix website using code.

What changed in 2026

Wix has moved site coding from Velo to a unified JavaScript SDK. According to the Wix developer docs, you can now use the SDK for most functionality, and Wix offers a migration guide from Velo. Existing Velo code keeps working, so the example below still runs, but new projects are better started with the SDK.

  • Where to code: the built-in Code panel in Wix Studio or the Wix Editor, the Wix IDE (a VS Code-based online editor for Wix Studio sites) or your own IDE connected through GitHub and the Wix CLI.

  • Wix Harmony doesn't support site code. Wix says that Harmony, its newest editor, doesn't support site code; you can only add HTML, JavaScript and CSS snippets as custom code. For tutorials like this one, use Wix Studio or the Wix Editor.

  • AI help: the Wix IDE includes an AI assistant for code, which is handy for adapting this example to your own elements.

Example description

In this example, I created a quiz that randomly selects 5 questions from a database. Once a website visitor answers all the quiz questions and submits them, we check their answers, show the correct answer for each question, and display the final result.

Wix quiz results page showing score and correct answers

Video tutorial on creating the code

Code example

Page code

import { getRandomQuestions } from 'backend/questions';
import wixWindow from 'wix-window';
const numOfQuizQuestions = 5;
let isUserAnswerValid;
$w.onReady(function () {
    setUpEventHandlers();
});
function setUpEventHandlers() {
    $w('#startQuizBtn').onClick(async () => {
        $w('#submitBtn').expand();
        $w('#welcomeGroup').collapse();
        $w('#scoreGroup').collapse();
        await initQuestionsData();
        $w('#quizGroup').expand();
    });
    $w('#submitBtn').onClick(async () => {
        isUserAnswerValid = true;
        $w('#invalidMessageText').collapse();
        await checkValidity();
        if (isUserAnswerValid) {
            await submitAnswers();
            $w('#scoreGroup').expand();
            $w('#welcomeText').text = 'Lets try again';
            $w('#welcomeGroup').expand();
        } else {
            $w('#invalidMessageText').expand();
        }
        wixWindow.scrollTo(0, 0);
    });
}
async function initQuestionsData() {
    const questions = await getRandomQuestions(numOfQuizQuestions);
    $w('#quizRepeater').data = questions;
    $w('#quizRepeater').onItemReady(($item, itemData, index) => {
        $item('#questionText').text = itemData.title;
        $item('#wrongAnswerText').hide();
        $item('#correctAnswerText').hide();
        $item('#correctAnswersGroup').hide();
        if (itemData.type === 'radioGroup') {
            $item('#radioGroup').required = false;
            $item('#radioGroup').selectedIndex = null;
            setRadioGroupOptions($item, itemData);
            $item('#radioGroup').expand();
            $item('#input').collapse();
        } else {
            $item('#input').required = false;
            $item('#input').value = null;
            $item('#input').expand();
            $item('#radioGroup').collapse();
        }
    });
}
function setRadioGroupOptions($item, itemData) {
    let opts = [];
    itemData.answersList.forEach((answer) => {
        opts.push({ 'label': answer, 'value': answer });
    })
    $item('#radioGroup').options = opts;
}
async function checkValidity() {
    $w('#quizRepeater').forEachItem(($item, itemData, index) => {
        if (itemData.type === 'input') {
            $item('#input').required = true;
            isUserAnswerValid = (isUserAnswerValid && $item('#input').valid);
        } else {
            $item('#radioGroup').required = true;
            isUserAnswerValid = (isUserAnswerValid && $item('#radioGroup').value);
        }
    });
}
async function submitAnswers() {
    $w('#submitBtn').collapse();
    let score = 0;
    $w('#quizRepeater').forEachItem(async ($item, itemData, index) => {
        let userAnswer;
        if (itemData.type === 'input') {
            userAnswer = $item('#input').value;
        } else {
            userAnswer = $item('#radioGroup').value;
        }
        if (userAnswer.toLowerCase() === itemData.correctAnswer.toLowerCase()) {
            score = score + 20;
            $w('#score').text = String(score);
            $item('#correctAnswerText').show();
        } else {
            $item('#wrongAnswerText').show();
        }
        $item('#correctAnswer').text = itemData.correctAnswer;
        $item('#correctAnswersGroup').show();
    });
}

Backend module questions.jsw

import wixData from 'wix-data';
const options = {
    'suppressAuth': true
};
async function getAllQuestions() {
    try {
        const request = await wixData.query('questions').find(options);
        if (request.items.length > 0) {
            return request.items;
        } else {
            throw 'no items found';
        }
    } catch (error) {
        console.error('an error was found in getAllQuestions function', error);
    }
}
export async function getRandomQuestions(numOfQuestions) {
    const allQuestions = await getAllQuestions();
    const numberOfItems = allQuestions.length;
    let randomQuestions = [];
    while (randomQuestions.length < numOfQuestions) {
        const randomNumber = Math.floor(Math.random() * (numberOfItems));
        let question = allQuestions[randomNumber];
        if (randomQuestions.indexOf(question) < 0) {
            randomQuestions.push(question);
        }
    }
    return randomQuestions;
}

How to create a similar quiz

Collection

I created a questions collection that stores all the questions and visitors’ answers.

Wix content manager showing Questions collection database

Page elements

I added the following elements:

  1. 3 text elements: to greet website visitors and display the quiz score.

  2. 2 buttons: a start quiz button and a submit button.

  3. Repeater: contains the questions and the possible answers to them. After submission, the repeater also shows the correct answer for each question and text indicating whether the visitor’s answer was correct or incorrect.


adding repeater element in Wix editor for quiz questions

Code

I added the question.jsw web module to retrieve information about questions and answers from the questions collection: getAllQuestions() is a function that queries the collection and returns all its items. getRandomQuestions() is a function that takes a variable indicating how many questions it should return. The function calls getAllQuestions() to retrieve all questions and returns the specified number of randomly selected questions. Page code In the $w.onReady function, we initialize the buttons’ onClick event handlers.

backend questions.jsw file in Velo by Wix code panel

When a website visitor clicks the “Start Quiz” button, we retrieve 5 random questions using the getRandomQuestions() function and bind them to the repeater’s data. When the submit button is clicked, we first check that the website visitor has answered all the quiz questions. If so, we validate the answers. We compare each answer with the correct answer obtained from the quiz data and display a message indicating whether each answer is correct or not. We also calculate the visitor’s score by adding 20 points for each correct answer.

Wix quiz FAQ

Can I build a quiz in Wix without code?

Yes, with Wix Forms or apps from the Wix App Market. You need Velo code only for scoring, branching questions or personalized results.

A quiz engages visitors, helps them choose the right service and collects contact details together with their answers — warmer leads than a plain form.

Usually 3–7. The shorter the quiz, the more people finish it and leave their details.

Answers from Wix Forms are saved in your Wix Inbox and can be emailed to you. You can also send them to a CRM with automations.

SEO MAMA is an official Wix Legend partner. We build and optimize Wix sites, from editor setup to SEO. Learn more: Wix SEO services.

Julia_edited_edited.jpg

Have a question?

Contact us

Contact us

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Recent posts

Speak AI Review: AI Transcription and Translation for Audio and Video

Speak AI Review: AI Transcription and Translation for Audio and Video

29 Nov 2026

Sales Funnel for a Service Website: Stages, Examples and Setup

Sales Funnel for a Service Website: Stages, Examples and Setup

29 Nov 2026

How Artificial Intelligence Works: A Simple Explanation for Business

How Artificial Intelligence Works: A Simple Explanation for Business

29 Nov 2026

The Best AI Content Generators in 2026: Review and Comparison

The Best AI Content Generators in 2026: Review and Comparison

29 Nov 2026

bottom of page