-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgindex.js
67 lines (54 loc) · 2.19 KB
/
gindex.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
(function (){
var url = "http://undefined2055.eu/GlycemicIndex/foodTables.json";
var request = new XMLHttpRequest();
request.onload = requestListener();
request.open("GET", url);
request.send(null);
function requestListener() {
function showFoods() {
var foodTables = JSON.parse(request.responseText);
var newInput = document.getElementById("textInput").value.toLowerCase(),
matchingFoods = [],
resultsList = document.getElementById("results");
// iterate throught JSON table objects and
//each time user input matches object name property, insert that object into new table matchingFoods
for(var i = 0, foodTableLength = foodTables.length; i<foodTableLength; i++){
if(newInput !== "" && foodTables[i].name.toLowerCase().indexOf(newInput) !== -1){
matchingFoods.push({
foodName: foodTables[i].name,
gIndex: foodTables[i].GI
});
}
}
//don't display anything when user deleted his input
while (resultsList.hasChildNodes()) {
resultsList.removeChild(resultsList.firstChild);
}
// generate new div for each case when user input matches any name of JSON table object
matchingFoods.map(function(item){
if(item.foodName.toLowerCase().indexOf(newInput)!==-1){
var element = document.createElement("div");
// and assign one of three colors to each of these divs
function changeColor(){
var className;
if(item.gIndex<36){
element.className = "gindex gindex-low";
}else if(item.gIndex<56){
element.className = "gindex gindex-medium";
}else{
element.className = " gindex gindex-high";
}
return element.className;
}
element.innerHTML = "<div style="+changeColor()+">"+
'<span class = "displayedName">' +item.foodName+ '</span>'+
" "+
'<span class = "displayedIndex">' +item.gIndex+ '</span>'+
"</div>";
resultsList.appendChild(element)
}
})
}
document.getElementById("textInput").oninput = showFoods;
}
})()