If you are looking for an MCQ quiz script, then you have come to the right place. In this article, I will show you how to add a responsive MCQ quiz section to your Blogger and WordPress websites.
When running a quiz website, you want your quizzes to be engaging and provide value to your readers. That’s why I have designed a simple quiz block using HTML, CSS, and JavaScript. This script includes pagination features, allowing users to navigate between multiple pages with previous and next buttons.
This approach lets you host many questions on a single blog post without making the page excessively long. Additionally, you can load more quizzes with a button click. I also implemented a feature where you can display a detailed explanation of the answer once the user selects an option.
So, let’s check how you can add the quiz section to your Blogger and WordPress websites.
Steps to Add MCQ Quiz Section with Pagination
To add the quiz section, use the code snippet below on your website. If you are using the Blogger CMS platform, edit the post in HTML view. If you are using WordPress, you can use the custom HTML block in your Gutenberg editor.
<div id="quiz-container">
<div class="question-container" data-page="1">
<h2 class="question">What is the capital of France?</h2>
<div class="quiz-form">
<div class="option" data-answer="true">Paris</div> <!-- Correct Answer -->
<div class="option" data-answer="false">Rome</div>
<div class="option" data-answer="false">Berlin</div>
<div class="option" data-answer="false">Madrid</div>
</div>
<div class="result"></div>
<div class="explanation">Paris is the capital of France.</div>
</div>
<div class="question-container" data-page="1">
<h2 class="question">Who wrote "Romeo and Juliet"?</h2>
<div class="quiz-form">
<div class="option" data-answer="false">Charles Dickens</div>
<div class="option" data-answer="true">William Shakespeare</div> <!-- Correct Answer -->
<div class="option" data-answer="false">Jane Austen</div>
<div class="option" data-answer="false">Mark Twain</div>
</div>
<div class="result"></div>
<div class="explanation">William Shakespeare wrote "Romeo and Juliet".</div>
</div>
<div class="question-container" data-page="2">
<h2 class="question">What does the abbreviation HTML stand for?</h2>
<div class="quiz-form">
<div class="option" data-answer="false">HyperText Markup Language.</div>
<div class="option" data-answer="true">HighText Markup Language.</div> <!-- Correct Answer -->
<div class="option" data-answer="false">HyperText Markdown Language.</div>
<div class="option" data-answer="false">None of the above.</div>
</div>
<div class="result"></div>
<div class="explanation">HTML stands for HighText Markup Language.</div>
</div>
<!-- Add more Questions here -->
</div>
<div class="pagination">
<button id="prevButton">Previous</button>
<button class="pageButton" data-page="1">1</button>
<button class="pageButton" data-page="2">2</button>
<!-- Add more page buttons as needed -->
<button id="nextButton">Next</button>
</div>
CSS & Javascript code
<style>
.question-container {
width: 100%;
margin: auto;
text-align: left;
border: 1px solid #ccc;
padding: 20px;
border-radius: 10px;
background-color: #f9f9f9;
margin-bottom: 20px;
}
.quiz-form {
display: flex;
flex-direction: column;
align-items: center;
}
.option {
margin-bottom: 10px;
padding: 10px;
cursor: pointer;
border: 1px solid #ccc;
background: #f3f3f3;
width:95%;
}
.option:hover {
background-color: #fff;
}
.result {
font-weight: bold;
margin-top: 20px;
}
.explanation {
margin-top: 10px;
display: none;
}
/* Pagination button styling */
.pagination {
margin-top: 20px;
text-align: center;
}
.pagination button {
background-color: #007bff;
color: #fff;
border: none;
padding: 10px 20px;
margin: 0 5px;
border-radius: 5px;
cursor: pointer;
}
.pagination button:hover {
background-color: #0056b3;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
const questionContainers = document.querySelectorAll('.question-container');
questionContainers.forEach(function(container) {
const options = container.querySelectorAll('.option');
const resultDisplay = container.querySelector('.result');
const explanationDisplay = container.querySelector('.explanation');
options.forEach(function(option) {
option.addEventListener('click', function() {
const selectedAnswer = option.getAttribute('data-answer');
const isCorrect = selectedAnswer === "true";
options.forEach(function(opt) {
opt.style.border = '1px solid #ccc';
});
option.style.border = isCorrect ? '1px solid #28a745' : '1px solid #dc3545';
option.style.background = isCorrect ? '#d8ffe1' : '#ffdbde';
resultDisplay.textContent = isCorrect ? "Correct!" : "Wrong!";
explanationDisplay.style.display = "block";
});
});
});
});
document.addEventListener('DOMContentLoaded', function() {
const questionContainers = document.querySelectorAll('.question-container');
let currentPageIndex = 1; // Track the current page index
function showQuestionsForPage(pageIndex) {
questionContainers.forEach(function(container, index) {
if (container.dataset.page === pageIndex.toString()) {
container.style.display = "block";
} else {
container.style.display = "none";
}
});
}
showQuestionsForPage(currentPageIndex);
const prevButton = document.getElementById('prevButton');
const nextButton = document.getElementById('nextButton');
const pageButtons = document.querySelectorAll('.pageButton');
prevButton.addEventListener('click', function() {
if (currentPageIndex > 1) {
currentPageIndex--;
showQuestionsForPage(currentPageIndex);
}
});
nextButton.addEventListener('click', function() {
if (currentPageIndex < questionContainers.length - 1) {
currentPageIndex++;
showQuestionsForPage(currentPageIndex);
}
});
pageButtons.forEach(function(button) {
button.addEventListener('click', function() {
currentPageIndex = parseInt(button.dataset.page);
showQuestionsForPage(currentPageIndex);
});
});
});
</script>
Note that if you want to use the script on all pages, you can directly add the CSS and JavaScript code to your theme code to load it site-wide. Then, use only the HTML code in the blog post.
However, I recommend using the script directly in the blog post if you are using the quiz on limited pages only.
I hope you have successfully added an MCQ Quiz Section with Pagination to your Blogger and WordPress websites. If you have any doubts, feel free to ask in the comment section.

Post a Comment