forked from avinoamsn/csci1010-assign6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.js
executable file
·25 lines (25 loc) · 1.06 KB
/
hello.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
/*
Listens to drop down list selection and updates the text and icon
based on the corresponding JSON file. If the JSON file does not
exist, instead set it to the Hello World default.
*/
window.addEventListener("load", function(e) {
document.querySelector('#selectList').addEventListener("change", function(evt) {
// The drop down list
var selectList = document.querySelector("#selectList");
// The selected ID
var selectListValue = selectList.options[selectList.selectedIndex].value;
// Finds the ID variable in the .json files
var variable = window[selectListValue];
// If the .json file exists and is correct, it will update the text and the icon
// If not, it will default to the hello world text and icon
if( variable ) {
var data = JSON.parse(variable);
document.querySelector('#text').textContent = "Hello " +data[0].name+ "!";
document.querySelector('#icon').src= data[0].icon;
}else{
document.querySelector('#text').textContent = "Hello World!";
document.querySelector('#icon').src= "resources/hello_world.png";
}
});
});