JSFiddle - React, Tailwind, and code Playground
by boazmier
HTML
<body>
<header>
<h1>Web App - Quiz</h1>
</header>
<section>
<div id="contain">
<h2 class="question">q1</h2>
<ul>
<li class="li0">
<input type="radio" value="ch1" name="choice" id="li0"/>
<label for="li0"></label>
</li>
<li class="li1">
<input type="radio" value="ch2" name="choice" id="li1"/>
<label for="li1"></label>
</li>
<li class="li2">
<input type="radio" value="ch3" name="choice" id="li2"/>
<label for="li2"></label>
</li>
<li class="li3">
<input type="radio" value="ch4" name="choice" id="li3"/>
<label for="li3"></label>
</li>
</ul>
<button id="next" type="submit">Next</button>
</div>
</section>
</body>
CSS
@import url(http://fonts.googleapis.com/css?family=Lily+Script+One);
body{
background: #f8f8f8 center no-repeat;
}
header{
position: relative;
top: 0;
left: 50%;
background: transparent center no-repeat;
width: 50%;
height: auto;
}
header h1{
font-family: 'Lily Script One', cursive;
font-size: 30px;
color: #000;
}
section{
position: relative;
top:0;
left: 25%;
opacity: 0.5;
background: #f1f1f1 center no-repeat;
padding: 3em;
width: 50%;
height: auto;
box-shadow: 5px 5px 5px #000;
font-family: "lily script one", cursive;
}
#contain{
background: transparent center no-repeat;
}
ul{
list-style: none;
}
JavaScript
var allQuestions = {
question: {
question0: {
question: "Who is the first Prime Minister of Israel?",
choices: ["David", "Rabin", "Peres", "Bibi"],
correct: "David"
},
question1: {
question: "What Is israel date of birth?",
choices: ["1948", "1958", "1950", "1944"],
correct: "1948"
},
question2: {
question: "What is PhoneGap framework for?",
choices: ["apache", "apps", "server", "client"],
correct: "apps"
}
},
correctAnswer: 0
};
var allquestion = allQuestions["question"];//just a quick way to access the object
var calcAnswers =allQuestions["correctAnswer"];//just a quick way to access the correctAnswer property object
var qNum = 0; //counts the questions up
var value; //undeclared var
//function that display the question and the choices to the section.
function loadquestions(qNum){
$(".question").text(allquestion["question"+qNum]["question"]); //set the the question to the title
for(var i=0;i<allquestion["question"+qNum]["choices"].length;){
//loops throughout the li's
var cH =(allquestion["question"+qNum]["choices"][i]); // a var that holds text of choice in poistion i
$("label[for=li"+i+"]").text(cH); //define for every label its text
$("#li"+i).attr("value",cH); //define for every input radio its value which is equals to label which is equals to var cH
i++;//count i with +1
}
}
//function that fires when clicked the "next" button and when trigered hides the current question and presents the next one
function checkClientChoice(){
$("#next").click(function(){
value = $("input[type='radio']:checked").val(); //gets the value of the choosen radio buttons
if(value == undefined){ //if the value is undefined throw an error and dont proceed to the next question
alert("Please choose a valid answer");
}
else {
...