-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex0.html
More file actions
53 lines (51 loc) · 1.77 KB
/
index0.html
File metadata and controls
53 lines (51 loc) · 1.77 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Getting Data From a Server | COMP1073 Client-Side JavaScript</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="css/normalize.css" rel="stylesheet" />
<link href="css/styles.css" rel="stylesheet" />
</head>
<body>
<h1>Getting Data From a Server | COMP1073 Client-Side JavaScript</h1>
<form>
<label for="verse-choose">Choose a very cool verse</label>
<select id="verse-choose" name="verse-choose">
<option value="verse1" selected>Verse 1</option>
<option value="verse2">Verse 2</option>
<option value="verse3">Verse 3</option>
<option value="verse4">Verse 4</option>
</select>
</form>
<h2>The Conqueror Worm, <em>Edgar Allen Poe, 1843</em></h2>
<pre>
</pre>
<script>
// STEP 1: Grab the HTML elements we need for the interaction
var verseChoose = document.querySelector('select');
var poemDisplay = document.querySelector('pre');
// STEP 2: Build out the event handler for the SELECT element
verseChoose.addEventListener('change', function(){
var verse = verseChoose.value;
// console.log(verse);
updateDisplay(verse);
});
// STEP 3: Construct updateDisplay() function
function updateDisplay(verse) {
// STEP 4: Declare and initialize URL to point to text file(s)
var url = verse + ".txt";
// STEP 5: Build fetch() with promises
fetch(url).then(function(response) {
response.text().then(function(text) {
poemDisplay.textContent = text;
});
});
};
// STEP 6: Initialize the app with Verse 1
updateDisplay('verse4');
verseChoose.value = 'verse4';
// This page inspired by and adapted from https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Fetching_data
</script>
</body>
</html>