回答編集履歴

1

JavaScriptについて、追加しました。

2020/04/19 07:25

投稿

new1ro
new1ro

スコア4528

test CHANGED
@@ -19,3 +19,107 @@
19
19
 
20
20
 
21
21
  `<button type="button" id="delete">Delete</button>`などを追加すればとりあえずエラーはなくなると思います。
22
+
23
+
24
+
25
+ ---
26
+
27
+ 追記しました。こちらでいかがでしょうか?
28
+
29
+ [CodePenで実装してみました](https://codepen.io/new1ro/pen/vYNKaLz)
30
+
31
+
32
+
33
+ ```JS
34
+
35
+ const myfunc = document.getElementById('click-function');
36
+
37
+ let count = 0;
38
+
39
+ myfunc.addEventListener('click',function(){
40
+
41
+ let todoItems = [];
42
+
43
+ let todoItem = document.getElementById('item').value;
44
+
45
+ todoItems.push(todoItem);
46
+
47
+ todoItems.forEach((element,index,array) => {
48
+
49
+ const btn_1 = document.createElement('button');
50
+
51
+ const btn_2 = document.createElement('button');
52
+
53
+ btn_1.textContent = '作業中';
54
+
55
+ btn_2.textContent = '消去';
56
+
57
+ btn_2.type = 'button';
58
+
59
+ btn_2.id = 'delete-' + count; /* idが重複しないようにする */
60
+
61
+
62
+
63
+
64
+
65
+ // 追加
66
+
67
+ btn_2.addEventListener('click',function(){
68
+
69
+ const list_item = document.getElementById('list_item');
70
+
71
+ const tr_count = document.getElementById('tr-'+count);
72
+
73
+ if(tr_count.hasChildNodes()){
74
+
75
+ list_item.removeChild(tr);
76
+
77
+ count--;
78
+
79
+ }
80
+
81
+ });
82
+
83
+
84
+
85
+
86
+
87
+ const td_1 = document.createElement('td');
88
+
89
+ const td_2 = document.createElement('td');
90
+
91
+ const td_3 = document.createElement('td');
92
+
93
+ const td_4 = document.createElement('td');
94
+
95
+ const tr = document.createElement('tr');
96
+
97
+ tr.id = "tr-" + count;
98
+
99
+ td_1.appendChild(document.createTextNode(count));
100
+
101
+ td_2.appendChild(document.createTextNode(todoItem));
102
+
103
+ td_3.appendChild(btn_1);
104
+
105
+ td_4.appendChild(btn_2);
106
+
107
+ tr.appendChild(td_1);
108
+
109
+ tr.appendChild(td_2);
110
+
111
+ tr.appendChild(td_3);
112
+
113
+ tr.appendChild(td_4);
114
+
115
+ document.getElementById('list_item').appendChild(tr);
116
+
117
+ count++;
118
+
119
+ })
120
+
121
+ });
122
+
123
+
124
+
125
+ ```