JSFiddle - React, Tailwind, and code Playground
HTML
<div id="quiz">
<label>
<p id="question"></p>
<select id="answers">
</select>
</label>
<button id="submit">Check Answer</button>
<p id="yourAnswer"></p>
</div>
JavaScript
var allQuestions = [{question: "Who is Prime Minister of the United Kingdom?",choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],correctAnswer: 0}];
document.getElementById('question').innerHTML = allQuestions[0].question;
for (i=0; i < allQuestions[0].choices.length; i++) {
var opt = document.createElement('option');
opt.innerHTML = allQuestions[0].choices[i];
document.getElementById('answers').appendChild(opt);
}
function checkAnswer(){
var myAnswer = document.getElementById('answers');
var answer = myAnswer.options[myAnswer.selectedIndex].value;
if( answer === 'David Cameron' ) {
document.getElementById('yourAnswer').innerHTML = 'Correct!';
$('#yourAnswer').css({
backgroundColor : '#78AB46',
fontWeight : 'bold',
color : '#fff',
borderColor : '#fff'
});
}
else {
document.getElementById('yourAnswer').innerHTML = 'Sorry that\'s incorrect';
$('#yourAnswer').css({
backgroundColor:'#EE3B3B',
fontWeight:'bold',
color : '#fff',
borderColor : '#fff'
});
}
}
var sub = document.getElementById('submit');
sub.addEventListener('click', checkAnswer, false);
/* Styling with jQuery for jQuery's sake */
/* Remember that dash '-' sperated property names need to be converted to camelCase */
$('#yourAnswer').css({
border : 'solid 1px #3f3f3f',
borderRadius : '3px',
backgroundColor : '#fff',
height : '2em',
paddingTop : '1em',
textAlign : 'center'
});
$('button').css({
display : 'block',
marginTop : '15px',
backgroundColor : '#2f2f2f',
color : '#efefef',
padding : '10px',
border : 'solid 1px #3f3f3f',
borderRadius : '5px'
});
$('#quiz').css({
padding : '20px',
border : 'solid 2px #6c6c6c',
borderRadius : '6px',
width : '400px',
backgroundColor : '#efefef',
fontFamily : 'arial'
});