JSFiddle - React, Tailwind, and code Playground
HTML
<link rel="stylesheet" href="http://necolas.github.io/normalize.css/3.0.1/normalize.css">
<body>
<div id="quizcontent" class="contentQuiz">
<h1 id="theQuestion">The Question?</h1>
<div id="answers">
<form id="genralCulture">
<input type="radio" value="0" name="quizAnswer" id="answer1" />
<label for="answer1" id="choice0">Answer1</label>
<!-- End Choice 1 -->
<input type="radio" value="1" name="quizAnswer" id="answer2" />
<label for="answer2" id="choice1">Answer2</label>
<!-- End Choice 2 -->
<input type="radio" value="2" name="quizAnswer" id="answer3" />
<label for="answer3" id="choice2">Answer3</label>
<!-- End Choice 3 -->
<input type="radio" value="3" name="quizAnswer" id="answer4" />
<label for="answer4" id="choice3">Answer4</label>
<!-- End Choice 4 -->
<button id="nextQuestion">Next Question</button>
</form>
</div>
</div>
<!-- End Quiz Content -->
<script src="js/dynamicQuiz.js"></script>
</body>
CSS
* {
margin: 0;
padding: 0;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
input[type="radio"] {
display: none;
}
input[type="radio"] + label:before {
background: #fff;
border: 1px solid #ccc;
content:"";
border-radius: 8px;
display: inline-block;
font-size: 24px;
width: 16px;
height: 16px;
line-height: 16px;
margin: -5px 10px 0 0;
vertical-align: middle;
text-align: center;
}
input[type="radio"]:checked + label:before {
content:"\2022";
background: #8fff00;
color: transparent;
}
.contentQuiz {
width: 70%;
height: auto;
margin: 150px auto;
padding: 1.5em;
border: 1px solid gray;
border-radius: 15px;
box-shadow: 0 3px 7px rgba(0, 0, 0, 0.60);
background: #f9f9f9;
text-align: center;
}
.contentQuiz h1 {
margin-bottom: 1.5em;
font: 36px/1.5em bold bolder'Helvetica Neue', Arial, sans-serif;
color: #00a1ff;
}
.contentQuiz label {
font: 24px/0.5em italic 100'Helvetica Neue LT Std', Verdana, serif;
color: #767676;
}
.contentQuiz button {
float: right;
border: none;
background: transparent;
font: 24px/50px'Bookman Old Style', Bookman, 'URW Bookman L', 'Palatino Linotype', serif;
color: #ffa400;
}
JavaScript
/**
* Dynamic Quiz
* @author Jeffrey Briceño
* @version 0.1
* @description Dynamic Quiz project from "http://javascriptissexy.com/how-to-learn-javascript-properly/"
*/
var allQuestions = [{
question: "What is the most populous country in the world?",
choices: ["China", "France", "United State", "Rusia"],
correctAnswer: 0
}, {
question: "What is the largest mammal in the world?",
choices: ["African Elephant", "Gorilla", "Black rhinoceros", "Blue Whale"],
correctAnswer: 3
}, {
question: "Who painted the ceiling of the Sistine Chapel?",
choices: ["Leonardo Da Vinci", "Michelangelo", "Piccazo", "Donnatelo"],
correctAnswer: 1
}, {
question: "Who was the writer of Alice's Adventures in Wonderland?",
choices: ["Lewis Carroll", "Rudyard Kipling", "J. K. Rowling", "stephen king"],
correctAnswer: 0
}, {
question: "Which scientist discovered the radioactive element radium?",
choices: ["Isaac Newton", "Albert Einstein", "Benjamin Franklin", "Marie Curie"],
correctAnswer: 3
}];
var currenQuestion = 0,
score = 0;
function checkAswer() {
//var button = document.getElementById('nextQuestion');
var radios = document.forms['genralCulture'].elements['quizAnswer'];
for (var j = 0, len = radios.length; j < len; j++) {
radios[j].onclick = function () {
if (this.value == allQuestions[currenQuestion].correctAnswer) {
score += 1;
}
}
}
}
function getQuestion() {
checkAswer();
var question = document.getElementById('theQuestion');
var select = document.forms.genralCulture.elements.quizAnswer;
if (currenQuestion < allQuestions.length) {
question.innerHTML = allQuestions[currenQuestion].question;
for (var i = 0; i < allQuestions.length - 1; i++) {
var answer = document.getElementById('choice' + i);
answer.innerHTML = allQuestions[currenQuestion].choices[i];
}
} else {
...