I am very new to HTML and js, facing problems that I have struggled with for days. None of my attempts works. It is so hard for a novice to comprehend information found on the internet.
The goal is to replace the repeated div, i.e. <div class="page" id="page1">, <div class="page" id="page2">, <div class="page" id="page3"> and its iterable components (img scource 1~3, etc) with one "for loop" by JavaScript.
Plus, the response on each page need to be recorded in order to compute accuracy and show it on the last page. Please help me :( Thank you so much....
Here are the codes (see how it works on codepen: https://codepen.io/azaleawang/pen/mdMajNK?editors=1000 ):
<script>
var order = new Array(1,2,3,4);
var current = 0; //current trial
$(document).ready(function(){
ShowTrial(current);
});
function ShowTrial(t) {
$("#page" + order[t]).show();
var N = t+1; //counting from 1
$(".progress").text('('+N+'/'+order.length+')');
}
function NextTrial() {
$("#page" + order[current]).hide();
current++;
ShowTrial(current);
}
</script>
<style> .page {display: none;} </style>
Please indicate the sex of this face:
Here is the repeated div need to be replaced and record user's response (cannot figure out the way to do it QQ):
<div class="page" id="page1">
<img src=1.jpg><br>
<input type=radio name=f1 value=M> M
<input type=radio name=f1 value=F> F <br>
<a href="javascript:NextTrial()">Next</a>
<span class="progress"></span>
</div>
<div class="page" id="page2">
<img src=2.jpg><br>
<input type=radio name=f2 value=M> M
<input type=radio name=f2 value=F> F <br>
<a href="javascript:NextTrial()">Next</a>
<span class="progress"></span>
</div>
<div class="page" id="page3">
<img src=3.jpg><br>
<input type=radio name=f3 value=M> M
<input type=radio name=f3 value=F> F <br>
<a href="javascript:NextTrial()">Next</a>
<span class="progress"></span>
</div>
<div class="page" id="page4">
<h3>All done! Thanks!</h3>
</div>
This is what I've tried but failed....
<div class="page" id="page1">
</div>
<script>
document.write('Please indicate the sex of this face:');
count = ['1','2','3'];
var pageNo= document.getElementById('page1');
container = document.getElementById('page1');
for(i = 1; i < count.length+1; i++){
container.innerHTML+='<img src='+i+'.jpg><br>';
container.innerHTML+='<input type=radio name=f1 value=M> M'
container.innerHTML+='<input type=radio name=f1 value=F> F <br>'
container.innerHTML+='<a href="javascript:NextTrial()">Next</a> ';
container.innerHTML+='<span class="progress"></span>';
pageNo.id='page'+i;
}
</script>
<div class="page" id="page4">
<h3>All done! Thanks!</h3>
</div>