JSFiddle - React, Tailwind, and code Playground
by medusae
HTML
<body>
<div id="questions">
<div id="words"></div>
<input type="button" value="VOTE" onClick="checkQuestions()" />
</div>
</body>
JavaScript
var allQuestions = [{question: "Who is Prime Minister of the United Kingdom?", choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"], correctAnswer:0},
{question: "How many toes??", choices: ["1", "2", "3", "5"], correctAnswer:3}];
//you can access checkbox name through an array
//match array number to number in allQuestions array
$(document).ready(
function makeQuestions() {
//sort through all questions starting at 1
for(var p = 0; p<allQuestions.length; p++) { //set position of question array
for(var i=0; i<allQuestions[p]['choices'].length; i++){ //sort through the choices
var final = allQuestions[p]["choices"][i] ;
$('#words').append('<input type="radio" name="newEst">' + final + '</input>');
//increase the questions by one
}
p+=1;
}
} );
function checkQuestions() {
var payment = " ";
var lenG = document.getElementsByTagName("input").newEst.length;
var i;
for (var i=0; i<lenG; i++){
if (document.getElementsByTagName("input").newEst[i].checked) {
payment = document.getElementsByTagName("input").newEst[i].value;
break;
}
}
if (payment !== "") {
return true;
} else {
alert("You haven't entered anything!");
return false;
}
}