-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
82 lines (74 loc) · 2.49 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<ul>
<li><a href="audio.html">go to audio page.</a></li>
</ul>
<h1>The Element Object</h1>
<h2>The appendChild() Method</h2>
<ul id="myList">
<li>Coffee</li>
<li>Tea</li>
</ul>
<p>Click "Append" to append an item to the end of the list:</p>
<button onclick="appendFunction()">Append</button>
<input type="text" id="myText" value="Some text..." />
<p>
<label for="setColor">Select the background color: </label>
<select id="setColor">
<option value="">--Make a choice--</option>
<option value="red">Red</option>
<option value="beige">Beige</option>
<option value="lightblue">Light Blue</option>
<option value="white">white</option>
</select>
</p>
<script>
const select = document.querySelector('select');
select.addEventListener('change', setColor);
function setColor() {
const choice = select.value;
switch (choice) {
case 'red':
document.body.style.backgroundColor = 'red';
break;
case 'beige':
document.body.style.backgroundColor = 'beige';
break;
case 'lightblue':
document.body.style.backgroundColor = 'lightblue';
break;
case 'white':
document.body.style.backgroundColor = 'white';
default:
document.body.style.backgroundColor = 'white';
}
}
function appendFunction() {
// Create an "li" node:
const node = document.createElement('li');
// grab the text from the input textbox
const textstring = document.getElementById('myText').value;
//console.log(textstring);
// Create a text node:
const textnode = document.createTextNode(textstring);
//console.log(textnode);
// Append the text node to the "li" node:
node.appendChild(textnode);
// Append the "li" node to the list:
document.getElementById('myList').appendChild(node);
// make an array of the list items
const liArray = document.body.getElementsByTagName('li');
// loop over the elements of the array and show them
for (const e of liArray) {
console.log(e.innerHTML);
}
}
</script>
</body>
</html>