Skip to content

Instantly share code, notes, and snippets.

@Aupajo
Created September 26, 2013 00:43
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Aupajo/6708312 to your computer and use it in GitHub Desktop.
Save Aupajo/6708312 to your computer and use it in GitHub Desktop.
Simple jQuery multiple-choice quiz
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple Quiz</title>
</head>
<body>
<form action="/success-page" class="simple-quiz">
<h3>Question 1</h3>
<label>
<input type="radio" name="answers[1]" value="A">
Answer A
</label>
<label>
<input type="radio" name="answers[1]" value="B">
Answer B
</label>
<label>
<input type="radio" name="answers[1]" value="C" data-correct>
Answer C
</label>
<h3>Question 2</h3>
<label>
<input type="radio" name="answers[2]" value="A">
Answer A
</label>
<label>
<input type="radio" name="answers[2]" value="B" data-correct>
Answer B
</label>
<label>
<input type="radio" name="answers[2]" value="C">
Answer C
</label>
<input type="submit">
<form>
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.simpleQuiz.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('.simple-quiz').simpleQuiz();
});
</script>
</body>
</html>
$.fn.every = function(callback) {
var numElements = this.length;
return this.filter(callback).length == numElements;
};
$.fn.simpleQuiz = function(options) {
if(!this.length) { return; };
this.each(function() {
var form = $(this);
var submitButton = form.find(':submit');
var questions = form.find('.question');
var choices = form.find(':radio');
var init = function() {
choices.on('change', answerChanged);
form.on('submit', answersSubmitted);
answerChanged();
};
var answersSubmitted = function(event) {
if(!hasPassed()) {
event.preventDefault();
alert('Please try again.');
}
};
var score = function() {
return form.find(':checked[data-correct]').length;
};
var hasPassed = function() {
return score() == questions.length;
};
var hasCheckedElement = function() {
return $(this).has(':checked').length;
};
var allQuestionsAnswered = function() {
return questions.every(hasCheckedElement);
};
var answerChanged = function() {
if(allQuestionsAnswered()) {
submitButton.removeAttr('disabled');
} else {
submitButton.attr('disabled', 'disabled');
}
};
init();
});
};
@imranr14
Copy link

imranr14 commented Dec 7, 2017

Hey, i need to create a quiz on my website using Wix Code, can anybody help me out there?
I dont know much about coding

@xbbnq7k
Copy link

xbbnq7k commented Jun 5, 2019

After clicking 'Submit', it throws File not found error in 'file:///success-page?answers%5B1%5D=B&answers%5B2%5D=C'

@Aupajo
Copy link
Author

Aupajo commented Jun 5, 2019

@xbbnq7k That'll be because you're running the page in a browser (e.g. the URL is file://). This script assumes you're running on an HTTP server (e.g. http://mysite.com/form with some http://mysite.com/success-page route capable of handling the data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment