#5 - image select
by ricoshae
HTML
<script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.css">
<h2>
Answer the following question by clicking on the appropriate image.
</h2>
<div id="riquestionbox" class="riitem clearfix">
<span id="riquestiontext">
</span>
</div>
<table class="table riactivity" style="width:100%">
<tr>
<td class="td1">
<img id="riimage001" name="Bruno Mars" src="https://ricoshae.com.au/assets/brunomars-wikimedia.jpeg"></td>
<td class="td2">
<img id="riimage002" name="Katy Perry" src="https://ricoshae.com.au/assets/katyperry-wikimedia.jpeg">
</td>
<td class="td3">
<img id='riimage003' name="Phil Collins" src="https://ricoshae.com.au/assets/philcollins-wikimedia.jpeg"></td>
</tr>
</table>
<div id="riquestionresponse" class="rihide"></div>
<button id="rinextquestion" class="rihide pull-right clearfix">
Next Question
</button>
<button id="rireset" class="pull-right rihide clearfix">
Re-start
</button>
CSS
.rihide {
display: none;
}
.riactivity img
{
cursor:pointer;
width:100%
}
.ricorrect {
position: relative;
padding: .75rem 1.25rem;
margin: 1rem;
border: 1px solid transparent;
border-radius: .25rem;
color: #155724;
background-color: #d4edda;
border-color: #c3e6cb;
width: 100%
}
.riincorrect {
position: relative;
padding: .75rem 1.25rem;
margin: 1rem;
border: 1px solid transparent;
border-radius: .25rem;
color: #721c24;
background-color: #f8d7da;
border-color: #f5c6cb;
width: 100%
}
.riitem {
position: relative;
padding: .75rem 1.25rem;
margin: 1rem;
border: 1px solid transparent;
border-radius: .25rem;
color: #111;
background-color: #efefef;
border-color: #dfdfdf;
width: 100%
}
JavaScript
// create an array and add some questions and answes
// note: arrays are zero based. That means array [0] is the first one
var currentquestion = 0;
var questions = [{
"question": "Who sang 'Fireworks'?",
"response": "Katy Perry",
"answerimage": 'riimage002'
}, {
"question": "Who is Peter Gene Hernandez ",
"response": "Peter Gene Hernandez is know as Bruno Mars",
"answerimage": 'riimage001'
}, {
"question": "Who was both lead singer and drummer in the same band?",
"response": "Phil Colins was the drummer, then lead singer for the band Genesis.",
"answerimage": 'riimage003'
}];
(function() {
// your page initialization code here
fnReset(); //call reset on document ready
document.getElementById("rinextquestion").addEventListener('click', function(event) {
fnNext();
});
document.getElementById("riimage001").addEventListener('click', function(event) {
fnCheck("riimage001", this);
});
document.getElementById("riimage002").addEventListener('click', function(event) {
fnCheck("riimage002", this);
});
document.getElementById("riimage003").addEventListener('click', function(event) {
fnCheck("riimage003", this);
});
document.getElementById("rireset").addEventListener('click', function(event) {
fnReset();
});
})();
function fnReset() {
document.getElementById('riquestionresponse').classList.add('rihide');
document.getElementById('rinextquestion').classList.add('rihide');
document.getElementById('rireset').classList.add('rihide');
document.getElementById('riquestionbox').classList.remove('rihide');
currentquestion = 0;
document.getElementById('riquestiontext').innerHTML = questions[currentquestion].question;
}
function fnNext() {
document.getElementById('riquestionresponse').classList.add('rihide');
if (currentquestion < questions.length - 1) {
currentquestion = currentquestion + 1;
document.getElementById('rinextquestion').classList.add('rihide');
...