teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

コード追記

2021/07/31 18:02

投稿

gah2s
gah2s

スコア9

answer CHANGED
@@ -21,4 +21,41 @@
21
21
  ```
22
22
 
23
23
  よく見ると、削除の確認をするまえに、`currentNote.remove()` が実行されてしまっています。
24
- なので、削除しない判断をした場合でも、削除ボタンをクリックした時点で当該ノードが消えてしまいます。
24
+ なので、削除しない判断をした場合でも、削除ボタンをクリックした時点で当該ノードが消えてしまいます。
25
+
26
+
27
+ 変更したコード
28
+ ```javascript
29
+ // Event: Note Buttons
30
+ noteContainer.addEventListener('click', (e) => {
31
+ if (e.target.classList.contains('note__view')) {
32
+ const currentNote = e.target.closest('.note');
33
+ const currentTitle = currentNote.querySelector('.note__title').textContent;
34
+ const currentBody = currentNote.querySelector('.note__body').textContent;
35
+ activateNoteModal(currentTitle, currentBody);
36
+ }
37
+ if (e.target.classList.contains('note__delete')) {
38
+ const currentNote = e.target.closest('.note');
39
+ const id = currentNote.querySelector('span').textContent;
40
+ removeNote(currentNote, Number(id))
41
+ }
42
+ })
43
+
44
+ // Function: remove a note from local storage
45
+ function removeNote(currentNote, id) {
46
+ if (window.confirm('削除しますか?') == true) {
47
+ const notes = getNotes();
48
+ notes.forEach((note, index) => {
49
+ if (note.id === id) {
50
+ notes.splice(index, 1);
51
+ }
52
+ localStorage.setItem('noteApp.notes', JSON.stringify(notes));
53
+ });
54
+ showAlertMessage('Your note was permanently deleted', 'remove-message');
55
+ currentNote.remove();
56
+ } else {
57
+ window.alert('操作がキャンセルされました。')
58
+ }
59
+ }
60
+ ```
61
+ removeNote に 新しく currentNote 引数を追加して、削除する選択の場合は、`currentNote.remove()` が動きます。

2

解釈変更

2021/07/31 18:02

投稿

gah2s
gah2s

スコア9

answer CHANGED
File without changes

1

解釈変更

2021/07/31 16:58

投稿

gah2s
gah2s

スコア9

answer CHANGED
@@ -21,4 +21,4 @@
21
21
  ```
22
22
 
23
23
  よく見ると、削除の確認をするまえに、`currentNote.remove()` が実行されてしまっています。
24
- なので、削除しない判断をした場合でも、当該ノードが消えてしまいます。
24
+ なので、削除しない判断をした場合でも、削除ボタンをクリックした時点で当該ノードが消えてしまいます。