forked from akatz6/dom_and_events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (80 loc) · 2.08 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const app = document.getElementById("app");
app.style.width = "80%";
app.style.margin = "0 auto";
const form = document.getElementsByTagName("form")[0];
form.style.width = "80%";
form.style.margin = "0 auto";
const arrSport = [
{
name: "Cubs",
city: "Chicago",
sport: "Baseball",
},
{
name: "Bears",
city: "Chicago",
sport: "Football",
},
{
name: "Bulls",
city: "Chicago",
sport: "Basketball",
},
{
name: "Blackhawks",
city: "Chicago",
sport: "hockey",
},
];
const table = document.createElement("table");
table.className = "table";
app.appendChild(table);
const thead = document.createElement("thead");
table.appendChild(thead);
const tr = document.createElement("tr");
thead.appendChild(tr);
const thName = document.createElement("th");
thName.innerHTML = "Name";
tr.appendChild(thName);
const thCity = document.createElement("th");
thCity.innerHTML = "City";
tr.appendChild(thCity);
const thSport = document.createElement("th");
thSport.innerHTML = "Sport";
tr.appendChild(thSport);
const tbody = document.createElement("tbody");
table.appendChild(tbody);
const display = () => {
arrSport.forEach((element) => {
const trBody = document.createElement("tr");
tbody.appendChild(trBody);
const tdName = document.createElement("td");
tdName.innerHTML = element.name;
trBody.appendChild(tdName);
const tdCity = document.createElement("td");
tdCity.innerHTML = element.city;
trBody.appendChild(tdCity);
const tdSport = document.createElement("td");
tdSport.innerHTML = element.sport;
trBody.appendChild(tdSport);
});
};
const addToArray = (e) => {
e.preventDefault();
const sport = document.getElementById("sport").value;
const city = document.getElementById("city").value;
const team = document.getElementById("team").value;
const obj = {
name: team,
city,
sport,
};
arrSport.push(obj);
for (let i = 1; i < table.rows.length; ) {
table.deleteRow(i);
}
display();
};
const button = document.getElementById("button");
button.addEventListener("click", addToArray);
display();