This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
likert.js
87 lines (77 loc) · 2.46 KB
/
likert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var qfile = 'questions'; // json file with questions in
var xmlhttp = new XMLHttpRequest();
var questions = []; // stores question ids
var scaleMax = 7; // number of items on likert scale
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var qdata = JSON.parse(xmlhttp.responseText);
writeQuestions(qdata);
}
}
// write questions
function writeQuestions(data) {
var title = data.title;
var t = document.getElementById('title');
t.innerHTML = title;
var qdata = data.questions;
for(var i = 0; i < qdata.length; i++) {
showQuestion(qdata[i]);
}
}
function showQuestion(q){
var list = document.getElementById("questionList");
var item = document.createElement("li");
var question = document.createTextNode(q.question);
var likertList = document.createElement('ul');
likertList.className = 'likert';
var likLab1 = document.createElement('li');
likLab1.appendChild(document.createTextNode("Agree"));
likertList.appendChild(likLab1);
questions.push(q.id);
for (var i = 0; i < scaleMax; i++){
var likertItem = document.createElement('li');
var radio = document.createElement('input');
radio.type = "radio";
radio.name = q.id;
radio.value = i;
radio.id = qfile + q.id + '-' + i;
likertItem.appendChild(radio);
likertList.appendChild(likertItem);
}
var likLab2 = document.createElement('li');
likLab2.appendChild(document.createTextNode("Disagree"));
likertList.appendChild(likLab2);
item.appendChild(question);
item.appendChild(likertList);
list.appendChild(item);
}
function loadQuestions(){
xmlhttp.open("GET", qfile + '.json', true);
xmlhttp.send();
}
// report results to server...
function storeData(){
var now = new Date();
var selection = []
// find out which were selected
for (var i = 0; i < questions.length; i++){
for (var j = 0; j < scaleMax; j++){
var radioId = qfile + questions[i] + '-' + j;
var radioEl = document.getElementById(radioId);
if (radioEl.checked){
selection.push(radioId);
radioEl.checked = false;
}
}
}
console.log(selection);
// report selection:
if(nodejs){
socket.emit('questions', {
"time": now.getTime(),
"set": qfile,
"selection": selection.toString()
});
}
}
loadQuestions();