前提
初歩的な質問になります。
現在JavascriptでTodoリストを作成しております。
Javascriptからリストと削除ボタンを生成しましたが
削除ボタンに削除機能を与えるのに、どこにコードを書き始めたらいいのかわかりません・・・
現在は入力フォームに入れた内容のタスクが表示されるとともに
削除ボタンは生成(表示)される状態です。
削除ボタンに機能はついていない状態になります。
イベントリスナーを使って削除機能を実装するには
削除ボタンを生成したコードの中に記述したらよいのでしょうか?
実現したいこと
入力フォームに入れて表示されたタスクを
削除ボタンをクリックして削除したい。
現在はタスク生成とともに削除ボタンも生成(表示)される状態です。
削除ボタンに機能はついていない状態になります。
発生している問題・エラーメッセージ
試したことに記載しておりますが、
そのコードですと下記のエラーが発生してしまいます。
JavaScript.js:63 Uncaught TypeError: Cannot read properties of undefined (reading 'rowIndex') at createRemoveButton (JavaScript.js:63:24) at JavaScript.js:54:26 at Array.forEach (<anonymous>) at showTaskList (JavaScript.js:36:9) at addData (JavaScript.js:19:3) at HTMLButtonElement.<anonymous> (JavaScript.js:81:3)
該当のソースコード
html
1 2<!DOCTYPE html> 3<html lang="ja"> 4<head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <title>Todoリスト</title> 8</head> 9<body> 10 <h1>Todoリスト</h1> 11 <p> 12 <input type="radio" name="status" value="1" checked="checked">全て 13 <input type="radio" name="status" value="2">作業中 14 <input type="radio" name="status" value="3">完了 15 </p> 16 17 <table> 18 <thead> 19 <th>ID</th> 20 <th>コメント</th> 21 <th>状態</th> 22 <th></th> 23 </thead> 24 <tbody id="todo_lists"></tbody> 25 </table> 26 27 <h2>新規タスクの追加</h2> 28 <input id="add_task" type="text"/> 29 <button id="add_button" type="button">追加</button> 30 <script src="JavaScript.js"></script> 31</body> 32</html>
Javascript
1 2'use strict' 3// 必要なDOM要素を取得。 4const addButton = document.getElementById('add_button') //htmlから追加ボタンの値を取得 5const addTask = document.getElementById('add_task') //htmlから入力したタスクの値の取得 6const todoLists = document.getElementById('todo_lists') //htmlからリストの値の取得 7 8// デフォルト値で1を設定 9let id = 1 10 11// todoを保存する箱 12const tasks = [] 13 14//配列にオブジェクトとしてデータを追加する 15const addData = () => { 16 tasks.push({ 17 id: id, 18 title: addTask.value, 19 }) 20 showTaskList(tasks) 21 addTask.value = '' 22 id++ 23} 24 25const createStatusButton = () => { 26 const status = document.createElement('td') 27 const statusButton = document.createElement('button') 28 statusButton.innerText = '作業中' 29 status.appendChild(statusButton) 30 return status; 31} 32 33const createRemoveButton = () => { 34 const remove = document.createElement('td') 35 const removeButton = document.createElement('button') 36 removeButton.innerText = '削除' 37 remove.appendChild(removeButton) 38 return remove; 39} 40 41 42//タグを追加して出力する関数 43const showTaskList = () => { 44 todoLists.innerHTML = ''; 45 tasks.forEach((task, index) => { 46 const todoItem = document.createElement('tr') 47 const todoId = document.createElement('td') 48 const todoTitle = document.createElement('td') 49 50 todoTitle.innerHTML = task.title 51 todoId.innerHTML = index+1 52 53 todoLists.appendChild(todoItem) 54 todoItem.appendChild(todoId) 55 todoItem.appendChild(todoTitle) 56 57 const statusButton = createStatusButton() 58 todoItem.appendChild(statusButton) 59 todoItem.appendChild(statusButton) 60 61 const removeButton = createRemoveButton() 62 todoItem.appendChild(removeButton) 63 todoItem.appendChild(removeButton) 64 }) 65} 66 67//追加ボタンをクリック 68addButton.addEventListener('click', () => { 69 const task = addTask.value 70 addData(task) 71})
試したこと
他の方のコードを拝見し、下記のようなコードも試しましたが
const createRemoveButton = () => { ~~~
の中に入れるのかもよくわからず・・・
const createRemoveButton = (todoItem) => {
var index = todoItem.rowIndex - 1;
const remove = document.createElement('td')
const removeButton = document.createElement('button')
removeButton.innerText = '削除'
remove.appendChild(removeButton(todoItem))
removeButton.addEventListener('click', () => {
tasks.splice(index, 1);
showTaskList();
});
return remove;
};
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。