-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcreate_dom_dynamic.html
54 lines (49 loc) · 1.14 KB
/
create_dom_dynamic.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
<!DOCTYPE HTML>
<html>
<head>
<title>Create DOM elements dynamically</title>
<script>
/*
---------------
try the jsfiddle version: http://jsfiddle.net/rover/5pw0bumx/
---------------
*/
// wait for the DOM to load before you try to access it
document.addEventListener("DOMContentLoaded", function(event) {
// array of names
var students = [
"Ainsley",
"Ashley",
"Caroline",
"Chrisanthy",
"Clare",
"Dinos",
"Hochien",
"Jaclyn",
"Jie",
"Jie",
"Julian",
"Pierre",
"Sisa",
"Tanya",
"Yang"
];
// get the <ul> unordered list element
var studentUl = document.getElementById("student_list");
// loop through the names
for (var i=0; i < students.length; i++){
// create <li> list item
var myLi = document.createElement("li");
// put the name text inside the <li>
myLi.innerHTML = students[i];
// attach to the end of <ul>
studentUl.appendChild(myLi);
}
});
</script>
</head>
<body>
<h1>Class list</h1>
<ul id="student_list"></ul>
</body>
</html>