I am doing a quiz which is different from the usual multiple choice answers where one is required to just click on an answer.
This quiz is different in the sense that a checkbox is ticked next to some corresponding texts/words. A running total is then added up where both a numeric as well as percentage score are then given.
There are about 20 units covering different areas of knowledge, and some of them are a couple or so hundreds.
I would like to link and record all the scores across the 20 units/modules as well an overall score to be linked to a candidate, and also be able to retrieve the results at a later date, included what are ticked and not ticked. As I’ve mentioned prior. Some units have over a hundred checkboxes. In each module/units of the test, I Would like to be able to randomise the test for it to pick out about 15 or so from each checkbox list modules/units list and populate as the test of each module begins and progresses across the test as a whole.
It would also be great if i could assign special values/attribute to some of the checkboxes lists, so a certain portion/percentage of them must be included in the populated randomised list that’s generated.
I have laid out below some snippets of codes of three of the test modules, namely Schools, Vets and Dental Surgeries. I would be eternally grateful if anyone is able to modify them accordingly to achieve what I am after, some of what I am after, or near what I am after, as it may give me some starting points.
I am still relatively new to most, if not all things Javascript etc. All my codes for each module/units are in 20 separate files. So, a lot of repetitive codings. I can’t help thinking that there must be a much more efficient way I could have coded things.
Please excuse the pasting in of the long coding
Thanks in advance for any help are suggestions given
Schools
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Schools: Knowledge Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
/* Header Section */
header {
background-color: #D3D3D3;
text-align: center;
width: 100%;
position: fixed;
height: 160px;
top: 0px;
}
body{
font-family: Arial;
background-image: url("images/school.jpg"), url("images/school2.jpg"), url("images/school3.jpg"), url("images/school4.jpg");
background-size: 75%;
}
#stats-div {
float: right;
background-color: lightblue;
width: 400px;
margin-top: -110px;
margin-right: 25px;
padding: 10px;
}
/*****************/
body {
margin: 0px;
margin-top: 0px;
}
#wrapper {
margin: 0 auto;
border: 5px solid #000;
border-radius: 5px;
width: 900px;
text-align: center;
overflow: scroll;
padding: 20px 0;
height: auto;
margin-top: 200px;
}
#q-wrapper {
margin: 10px auto;
background-color: lightblue;
width: 800px;
border-radius: 3px;
padding: 10px 0px;
height: auto;
}
form {
margin-top: 30px;
text-align: left;
width: 800px;
height: auto;
overflow: auto;
}
h1, h2, h3 {
margin: 10px 0px;
}
input[type=checkbox] {
/* transform: scale(2.5); Causes super imposed problem with fixed/static header when scrolling up*/
padding: 0px;
margin: 0px;
width: 30px;
height: 18px;
}
}
.q-texts {
font-size: 20px;
color: #000;
margin: 15px;
}
#result {
font-weight: bold;
color: #000;
font-size: 20px;
}
</style>
</head>
<body onload="toTalQuestions()">
<header>
<h1>City Council</h1>
<h2>Locations Knowledge Mock Test:</h2>
<h3>Schools</h3>
<div id="stats-div">
<div id="result">Score = <span id="selected">0</span></div> <div id="notchecked">Remaing Questions or Not Answered = <span id="unselected"></span></div>
<!-- Counting only worked after JS code was moved from the 'head' tag to the bottom of the body tag -->
<div id="num-of-questions">Total Amount Of Question = <span id="totalquestions"></span></div>
<div><strong>Score Percentage</strong> <span id="score-perc" style="color: green; font-size: 1.5em;"></span> <strong>%</strong></div>
<div id="perScoreDiv">
</div>
<input type="button" id="tick-all-btn" onclick="checkAll2()" class="btn btn-link" value="Tick All Answers">
<input type="button" onclick="uncheckAll2()" class="btn btn-link" value="Reset Test">
</div>
</header>
<div id="wrapper">
<div id="q-wrapper">
<form>
<h6>Infants</h6>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Churchdown Infant School: Station Rd (Churchdon)</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Longlevens Infant School: Paygrove Ln</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Tredworth Infant School: Victory Rd</label><br>
<h6>Juniors</h6>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Churchdown Villiage Junior school: Station Rd (Churchdon)</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">St James Junior (C Of E): Upton St</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Longlevens Junior School: Church Rd</label><br>
<h6>Primaries</h6>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Finlay Community Primary School: Finlay Rd</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Beech Green Primary School: St James (Quedgeley)</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">St Paul’s Primary School (C of E): New St</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Linden Primary School: Linden Rd</label><br>
<h6>Seniors</h6>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Sir Thomas Rich’s School: Oakleaze</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Holmleigh High School (Formerly Beaufort Academy): Holmleigh Rd</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Ribston Hall High School: Stroud Rd</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">St Peter’s High School: Stroud Rd</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Churchdown School Academy: Winston Rd</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Chosen Hill School: Brookfield Rd</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">The Crypt School: Podsmead Rd</label><br>
<h6>Faith Schools</h6>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Al-Ashraf Primary School and Nursery: Stratton Rd</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Al-Ashraf Secondary School: Sinope St</label><br>
<h6>Steiner Schools</h6>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Wynstone School: Church Ln (Whaddon)</label><br>
<h6>PRUs</h6>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">Russett House: Russett Cl</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">The Raikes Centre: Horton Rd</label><br>
<h6>Misc</h6>
<h6>Private</h6>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">The King's School: Pitt St</label><br>
<input type="checkbox" class="check2" name="schools" /><label class="q-texts">OneSchool Campus (AKA Focus School): Eastbrook Rd</label><br>
</form>
</div>
</div>
<script>
// Score Percentage
function percentage() {
var totalqs = document.getElementById('totalquestions').innerText
var correct = document.getElementById('selected').innerText
scoreperc = (correct / totalqs); // Adjusted from original (http://forum.freecodecamp.org/t/nan-result-instead-of-percentage/299950)
document.getElementById("score-perc").innerHTML = Math.round(scoreperc * 100) // Adjusted from original
}
// Count Checked
const selectedElm = document.getElementById('selected');
function showChecked(){
selectedElm.innerHTML = document.querySelectorAll('input[name=schools]:checked').length;
percentage(); // Trigers call to the "percentage" function to either increment/decrement score percentage when a checkbox is either ticked or unticked
}
document.querySelectorAll("input[name=schools]").forEach(i=>{
i.onclick = () => showChecked();
});
// Reset test and Check all checkboxes
//v2 Tick all Checkboxes
function checkAll2() {
var inputs = document.querySelectorAll('.check2');
for(var i = 0; i < inputs.length; i++) {
inputs[i].checked = true;
}
}
// Untick All Checkboxes
function uncheckAll2() {
var inputs = document.querySelectorAll('.check2');
for(var i = 0; i < inputs.length; i++) {
inputs[i].checked = false;
}
}
// Count Unticked boxes
$(function () {
$("input:checkbox").on("change", function () {
var lenghtOfUnchecked = $('input:checkbox:not(:checked)').length;
var unticked = document.getElementById('unselected').innerHTML = lenghtOfUnchecked;
});
});
// Amount Of Checkboxe Questions
function toTalQuestions() {
var toTalQuestions = document.getElementsByName('schools').length;
document.getElementById('totalquestions').innerHTML = toTalQuestions;
}
</script>
</body>
</html>
Dental Surgeries
<!DOCTYPE html>
<!-- Has streets ouside boudary in csv -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dental Surgeries: Knowledge Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
/* Header Section */
header {
background-color: #D3D3D3;
text-align: center;
width: 100%;
position: fixed;
height: 160px;
top: 0px;
}
body{
font-family: Arial;
}
#stats-div {
float: right;
background-color: lightblue;
width: 400px;
margin-top: -110px;
margin-right: 25px;
padding: 10px;
}
/*****************/
body {
margin: 0px;
margin-top: 0px;
}
#wrapper {
margin: 0 auto;
border: 5px solid #000;
border-radius: 5px;
width: 900px;
text-align: center;
overflow: scroll;
padding: 20px 0;
height: auto;
margin-top: 200px;
}
#q-wrapper {
margin: 10px auto;
background-color: lightblue;
width: 800px;
border-radius: 3px;
padding: 10px 0px;
height: auto;
}
form {
margin-top: 30px;
text-align: left;
width: 800px;
height: auto;
overflow: auto;
}
h1, h2, h3 {
margin: 10px 0px;
}
input[type=checkbox] {
/* transform: scale(2.5); Causes super imposed problem with fixed/static header when scrolling up*/
padding: 0px;
margin: 0px;
width: 30px;
height: 18px;
}
}
.q-texts {
font-size: 20px;
color: #000;
margin: 15px;
}
#result {
font-weight: bold;
color: #000;
font-size: 20px;
}
</style>
</head>
<body onload="toTalQuestions()">
<header>
<h1>City Council</h1>
<h2>Locations Knowledge Mock Test:</h2>
<h3>Dental Surgeries</h3>
<div id="stats-div">
<div id="result">Score = <span id="selected">0</span></div> <div id="notchecked">Remaing Questions or Not Answered = <span id="unselected"></span></div>
<!-- Counting only worked after JS code was moved from the 'head' tag to the bottom of the body tag -->
<div id="num-of-questions">Total Amount Of Question = <span id="totalquestions"></span></div>
<div><strong>Score Percentage</strong> <span id="score-perc" style="color: green; font-size: 1.5em;"></span> <strong>%</strong></div>
<div id="perScoreDiv">
</div>
<input type="button" id="tick-all-btn" onclick="checkAll2()" class="btn btn-link" value="Tick All Answers">
<input type="button" onclick="uncheckAll2()" class="btn btn-link" value="Reset Test">
</div>
</header>
<div id="wrapper">
<div id="q-wrapper">
<form>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Abbeymead Dental Care: Abbeymead Ave</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Genix Healthcare Dental Clinic: Lister House (Station Rd)</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Roseville House Dental Practice: Hucclecote Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Bupa Dental Care Gloucester: Stroud Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Bupa Dental Care Gloucester: Painswick Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Bupa Dental Care Gloucester Westgate: Three cocks Ln</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">My Dentist: Eastgate House (Eastgate St)</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Gloucester Dental Care: London Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Acorn Dental Surgery: Barnwood Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Affinity Dental Care: Cheltenham Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">NHS Dental Access Centre: Kimbrose Way</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Clarence House Deantal Health Centre: Clarence St</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Cotteswold House Dental Care: Cotteswold Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Messenger Dental Practice: Messenger House (St Michael Square)</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">The Limes Dental Practice: Stroud Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Norfolk House Dental Care: London Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Courtlands Dental Surgery: Painswick Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Phoenix Orthodontics: Clarence St</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">The Community Dental: Kimbrose Hotel Commercial Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Dept Oral Maxillofacial & Orthodontics: GRH (Great Western Rd</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Quedgeley House Dental Practice: Space Park (Olympus Park)</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">St James Dental: St James (Quedgeley)</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">Windsor Drive Dental Practice: Windsor Dr</label><br>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">White Clinic Teeth Whitening In Gloucester: Westgate St</label><br>
<h3>Further Afield</h3>
<input type="checkbox" class="check2" name="dentists" /><label class="q-texts">St Johns Dental Surgery: Johns Ave (Churchdown)</label><br>
</form>
</div>
</div>
<script>
// Score Percentage
function percentage() {
var totalqs = document.getElementById('totalquestions').innerText
var correct = document.getElementById('selected').innerText
scoreperc = (correct / totalqs); // Adjusted from original (http://forum.freecodecamp.org/t/nan-result-instead-of-percentage/299950)
document.getElementById("score-perc").innerHTML = Math.round(scoreperc * 100) // Adjusted from original
}
// Count Checked
const selectedElm = document.getElementById('selected');
function showChecked(){
selectedElm.innerHTML = document.querySelectorAll('input[name=dentists]:checked').length;
percentage(); // Trigers call to the "percentage" function to either increment/decrement score percentage when a checkbox is either ticked or unticked
}
document.querySelectorAll("input[name=dentists]").forEach(i=>{
i.onclick = () => showChecked();
});
// Reset test and Check all checkboxes
//v2 Tick all Checkboxes
function checkAll2() {
var inputs = document.querySelectorAll('.check2');
for(var i = 0; i < inputs.length; i++) {
inputs[i].checked = true;
}
}
// Untick All Checkboxes
function uncheckAll2() {
var inputs = document.querySelectorAll('.check2');
for(var i = 0; i < inputs.length; i++) {
inputs[i].checked = false;
}
}
// Count Unticked boxes
$(function () {
$("input:checkbox").on("change", function () {
var lenghtOfUnchecked = $('input:checkbox:not(:checked)').length;
var unticked = document.getElementById('unselected').innerHTML = lenghtOfUnchecked;
});
});
// Amount Of Checkboxe Questions
function toTalQuestions() {
var toTalQuestions = document.getElementsByName('dentists').length;
document.getElementById('totalquestions').innerHTML = toTalQuestions;
}
</script>
</body>
</html>
VETS
<!DOCTYPE html>
<!-- Has streets ouside boudary in csv -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Vetinary Surgeries: Knowledge Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<style type="text/css">
/* Header Section */
header {
background-color: #D3D3D3;
text-align: center;
width: 100%;
position: fixed;
height: 160px;
top: 0px;
}
body{
font-family: Arial;
}
#stats-div {
float: right;
background-color: lightblue;
width: 400px;
margin-top: -110px;
margin-right: 25px;
padding: 10px;
}
/*****************/
body {
margin: 0px;
margin-top: 0px;
}
#wrapper {
margin: 0 auto;
border: 5px solid #000;
border-radius: 5px;
width: 900px;
text-align: center;
overflow: scroll;
padding: 20px 0;
height: auto;
margin-top: 200px;
}
#q-wrapper {
margin: 10px auto;
background-color: lightblue;
width: 800px;
border-radius: 3px;
padding: 10px 0px;
height: auto;
}
form {
margin-top: 30px;
text-align: left;
width: 800px;
height: auto;
overflow: auto;
}
h1, h2, h3 {
margin: 10px 0px;
}
input[type=checkbox] {
/* transform: scale(2.5); Causes super imposed problem with fixed/static header when scrolling up*/
padding: 0px;
margin: 0px;
width: 30px;
height: 18px;
}
}
.q-texts {
font-size: 20px;
color: #000;
margin: 15px;
}
#result {
font-weight: bold;
color: #000;
font-size: 20px;
}
</style>
</head>
<body onload="toTalQuestions()">
<header>
<h1>Gloucester City Council</h1>
<h2>Hackney Carriage Mock Test:</h2>
<h3>Vetinary Suegeries</h3>
<div id="stats-div">
<div id="result">Score = <span id="selected">0</span></div> <div id="notchecked">Remaing Questions or Not Answered = <span id="unselected"></span></div>
<!-- Counting only worked after JS code was moved from the 'head' tag to the bottom of the body tag -->
<div id="num-of-questions">Total Amount Of Question = <span id="totalquestions"></span></div>
<div><strong>Score Percentage</strong> <span id="score-perc" style="color: green; font-size: 1.5em;"></span> <strong>%</strong></div>
<div id="perScoreDiv">
</div>
<input type="button" id="tick-all-btn" onclick="checkAll2()" class="btn btn-link" value="Tick All Answers">
<input type="button" onclick="uncheckAll2()" class="btn btn-link" value="Reset Test">
</div>
</header>
<div id="wrapper">
<div id="q-wrapper">
<form>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Vets4Pets: Barnwood Rd</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Hucclecote Pets & Vets: Hucclecote Rd (AAS)</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Quedgeley Vetinary Surgery: Bristol Rd (Quedgeley) (AAS)</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Hucclecote Rd: Horseshoe Dr</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">SPA Veterniary Service: Painswick Rd</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">SPA Veterniary Service: School Ln (Quedgeley)</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Woods Vetinary Group Animal Hospital: Bristol Rd (Quedgeley)</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Woods Vetinary Group Animal Hospital: Windermere Rd</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Companion Care Vets: Woodrow Way (Inside Pets At Home)</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Simply Pets: Horton Rd</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">The Brambles Vetinary Surgery: Barnwood Rd</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Abbeydale Animal Hospital: Glevum Way</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Abbeydale Vetinary Surgery: Glevum Way (AAS)</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">British Small Animal Veterinary Association: Telford Way (BSAVA)</label><br>
<h3>Further Afield</h3>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">The Brambles Vetinary Surgery (Churchdown): Albermarle Rd</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">The Brambles Vetinary Surgery (Brockworth): Green St</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">The Brambles Vetinary Surgery (Churchdown): Albermarle Rd</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Arvonia Vets Churchdown Vetinary Centre: Cheltenham Rd East (Churchdown)</label><br>
<input type="checkbox" class="check2" name="vets" /><label class="q-texts">Kingsway Vetinary Clinic: Kingsway Local Centre</label><br>
</form>
</div>
</div>
<script>
// Score Percentage
function percentage() {
var totalqs = document.getElementById('totalquestions').innerText
var correct = document.getElementById('selected').innerText
scoreperc = (correct / totalqs); // Adjusted from original (http://forum.freecodecamp.org/t/nan-result-instead-of-percentage/299950)
document.getElementById("score-perc").innerHTML = Math.round(scoreperc * 100) // Adjusted from original
}
// Count Checked
const selectedElm = document.getElementById('selected');
function showChecked(){
selectedElm.innerHTML = document.querySelectorAll('input[name=vets]:checked').length;
percentage(); // Trigers call to the "percentage" function to either increment/decrement score percentage when a checkbox is either ticked or unticked
}
document.querySelectorAll("input[name=vets]").forEach(i=>{
i.onclick = () => showChecked();
});
// Reset test and Check all checkboxes
//v2 Tick all Checkboxes
function checkAll2() {
var inputs = document.querySelectorAll('.check2');
for(var i = 0; i < inputs.length; i++) {
inputs[i].checked = true;
}
}
// Untick All Checkboxes
function uncheckAll2() {
var inputs = document.querySelectorAll('.check2');
for(var i = 0; i < inputs.length; i++) {
inputs[i].checked = false;
}
}
// Count Unticked boxes
$(function () {
$("input:checkbox").on("change", function () {
var lenghtOfUnchecked = $('input:checkbox:not(:checked)').length;
var unticked = document.getElementById('unselected').innerHTML = lenghtOfUnchecked;
});
});
// Amount Of Checkboxe Questions
function toTalQuestions() {
var toTalQuestions = document.getElementsByName('vets').length;
document.getElementById('totalquestions').innerHTML = toTalQuestions;
}
</script>
</body>
</html>