JSFiddle - React, Tailwind, and code Playground
by David Oh
HTML
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<div id="wrap">
<div class ="container">
<div class="page-header">
<span class="glyphicon glyphicon-check"></span><br>
<h1>Let's Take a Quiz!</h1>
</div>
<div class="question">
<h2>Question #</h2>
<div class="clear"></div>
<div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="10" aria-valuemin="0" aria-valuemax="100" style="width: 0%"><span class="sr-only">55% Complete (success)</span>
</div>
</div><!-- /.progress -->
<div class="notchecked alert alert-danger"></div>
<p class="stem"></p>
<form>
<div class="container">
<ul class="options"></ul>
</div>
<div class="clear"></div>
<input type="submit" class="btn btn-success" value="Next Question >>">
</form>
</div><!-- /.question -->
<div class="results">
<h2>Let's See How You Did, Champion...</h2>
<div class=".clear"></div>
<div class="alert alert-success"></div>
</div><!-- /.results -->
</div><!-- /.container -->
</div><!-- /.wrap -->
<div id="footer">
<div class="container">
<p class="text-muted">The Bootstrap Quiz by Mike Fiorillo. Designed using Bootstrap, JavaScript, jQuery, HTML, and CSS.</p>
</div>
</div>
CSS
/* Sticky footer styles
-------------------------------------------------- */
html,
body {
height: 100%;
font-size: 24px;
font-family: 'Open Sans', sans-serif;
font-weight: 400;
/* The html and body elements cannot have any padding or margin. */
}
/* Wrapper for page content to push down footer */
#wrap {
min-height: 100%;
height: auto;
/* Negative indent footer by its height */
margin: 0 auto -50px;
/* Pad bottom by footer height */
padding: 0 0 60px;
}
/* Set the fixed height of the footer here */
#footer {
height: 60px;
background-color: #f5f5f5;
font-size: 12px;
}
/* Custom page CSS
-------------------------------------------------- */
/* Not required for template or sticky footer method. */
.container {
width: auto;
max-width: 680px;
padding: 0 15px;
text-align: center;
}
.container .text-muted {
margin: 20px 0;
}
/* My CSS */
h1, h2 {
font-family: 'Open Sans', sans-serif;
font-weight: 700;
margin-top: 0px;
}
label {
font-weight: 400;
}
.alert {
padding: 10px;
}
.glyphicon-check {
font-size: 48px;
}
.page-header {
text-align: center;
}
.progress {
width: 300px;
display: inline-block;
}
.results-row {
text-align: center;
}
.results {
display: inline-block;
}
h2 {
font-size: 18px;
}
ul {
list-style-type: none;
text-align: left;
display: inline-block;
}
li {
margin-left: -25px;
}
.question {
text-align: center;
}
input[type="radio"] {
margin-right: 20px;
}
JavaScript
$(document).ready(function() {
var questionList = [{question: "Who is Prime Minister of the United Kingdom?", choices: ["David Cameron",
"Gordon Brown", "Winston Churchill", "Tony Blair"], correctAnswer:0},
{question: "What is the capital of Sweden?",choices: [
"Helsinki", "Oslo", "St. Petersburg", "Stockholm"], correctAnswer:3},
{question: "Who wrote the Great Gatsby?", choices: [
"Ernest Hemmingway", "F. Scott Fitzgerald", "J. D. Salinger", "William Faulkner"], correctAnswer:1}];
var userAnswers = [];
var answerList = [];
$(".results").hide();
$(".alert").hide();
// Add correct answers to answerList array (not necessary and slightly inefficient, may remove later)
_.each(questionList, function (questionObject) {
answerList.push(questionObject.correctAnswer);
})
var theQuiz = {
numQuestions: questionList.length,
index: 0,
displayQuestion: function () {
var i = this.index;
// console.log(this.index)
var total = this.numQuestions;
$(".options").empty();
_.each(questionList[i].choices, function (item, choiceIndex) {
// $("text").appendTo($choice);
var $choice = $("<li><label><input type='radio' name='group"+ i +"' value=" + choiceIndex +"></label></li>");
$(".question h2").html("Question " + (i + 1) + " of " + total);
$(".stem").html(questionList[i].question);
$choice.find("label").append(item);
$choice.appendTo($(".options"));
})
this.index += 1;
if (i === (this.numQuestions - 1)) {
$('input[type="submit"]').val("Last Question! Finish the quiz.");
}
// button is in focus after submitting question, so blur it for cleaner look
$("button").blur();
$(".progress-bar").css("width", ((theQuiz.index - 1) / theQuiz.numQuestions)*100 + "%");
...