@@ -73,13 +73,8 @@ todoList.appendChild(li);
7373また、` HTMLInputElement#value ` プロパティから入力欄への入力内容を取得できます。
7474
7575``` js
76- addButton .onclick = () => {
77- const todoItem = document .createElement (" li" ); // li 要素を作る
78- const todoText = document .createElement (" span" ); // span 要素を作る
79- todoText .textContent = todoInput .value ; // span 要素の textContent を todoInput の value にする
80- todoInput .value = " " ; // todoInput の入力を空にする
81- // 以下 Node#appendChild などを使い、整形する
82- };
76+ todoText .textContent = todoInput .value ; // span 要素の textContent を todoInput の value にする
77+ todoInput .value = " " ; // todoInput の入力を空にする
8378```
8479
8580<Details summary = ' ステップ 1 の解答例' >
@@ -178,14 +173,12 @@ addButton.onclick = () => {
178173
179174編集ボタンをつけてみましょう。
180175
181- 編集ボタンが押されたときに ToDo のテキストを入力欄に、編集ボタンを確定ボタンに入れ替えます 。
182- [ ` Node#replaceChild ` メソッド ] ( https://developer.mozilla.org/ja/docs/Web/API/Node/replaceChild ) を使い、ある要素の子要素を別の要素にいれかえることができます 。
176+ 編集ボタンが押されたときに、ユーザーにテキストを入力するように促すダイアログを表示します 。
177+ [ ` prompt ` 関数 ] (https://developer.mozilla.org/ja/docs/Web/API/Window/prompt を使い、ユーザーに入力を求めることができます 。
183178
184179``` javascript
185- const confirmButton = document .createElement (" button" );
186- confirmButton .textContent = " 確定" ;
187- // 編集ボタンを確定ボタンに入れ替え
188- todoItem .replaceChild (confirmButton, editButton);
180+ // ユーザーに入力を求め、入力された値を todoText.textContent に代入する
181+ todoText .textContent = prompt (" 編集内容を入力してください" );
189182```
190183
191184<Details summary = ' ステップ 3 の解答例' >
@@ -288,7 +281,8 @@ addButton.onclick = () => {
288281 editButton .textContent = " 編集" ;
289282 editButton .onclick = () => {
290283 const input = prompt (" 編集内容を入力してください" );
291- if (input !== " " ) todoText .textContent = input;
284+ // prompt が空で入力された場合は空文字列 ("")、キャンセルした場合は null を返す
285+ if (input !== " " && input !== null ) todoText .textContent = input;
292286 };
293287 deleteButton .textContent = " 削除" ;
294288 deleteButton .onclick = () => {
0 commit comments