diff --git a/favorites.js b/favorites.js new file mode 100644 index 0000000..5f8db09 --- /dev/null +++ b/favorites.js @@ -0,0 +1,45 @@ +document.addEventListener('DOMContentLoaded', () => { + const favoritesList = document.getElementById('favorites-list'); + + // localStorage からデータを取得 + let favorites = JSON.parse(localStorage.getItem('favorites') || '[]'); + + // リストを描画する関数 + function renderList(items) { + favoritesList.innerHTML = ''; // リストをリセット + if (items.length === 0) { + favoritesList.innerHTML = '
  • 行きたいお店はまだ登録されていません。
  • '; + } + items.forEach((item, index) => { + const li = document.createElement('li'); + li.className = 'favorite-item'; + + // 店舗名と追加日時を表示 + li.innerHTML = ` + ${item.name} + ${new Date(item.addedAt).toLocaleString()} + + `; + + favoritesList.appendChild(li); + }); + + // 削除ボタンにイベントリスナーを追加 + document.querySelectorAll('.delete-btn').forEach(button => { + button.addEventListener('click', (event) => { + const index = event.target.dataset.index; + deleteFavorite(index); + }); + }); + } + + // 削除する関数 + function deleteFavorite(index) { + favorites.splice(index, 1); // 該当のデータを削除 + localStorage.setItem('favorites', JSON.stringify(favorites)); // 更新されたデータを localStorage に保存 + renderList(favorites); // 再描画 + } + + // 初期表示(追加日時順) + renderList(favorites); +}); diff --git a/favoritesgood.js b/favoritesgood.js deleted file mode 100644 index 5f8db09..0000000 --- a/favoritesgood.js +++ /dev/null @@ -1,45 +0,0 @@ -document.addEventListener('DOMContentLoaded', () => { - const favoritesList = document.getElementById('favorites-list'); - - // localStorage からデータを取得 - let favorites = JSON.parse(localStorage.getItem('favorites') || '[]'); - - // リストを描画する関数 - function renderList(items) { - favoritesList.innerHTML = ''; // リストをリセット - if (items.length === 0) { - favoritesList.innerHTML = '
  • 行きたいお店はまだ登録されていません。
  • '; - } - items.forEach((item, index) => { - const li = document.createElement('li'); - li.className = 'favorite-item'; - - // 店舗名と追加日時を表示 - li.innerHTML = ` - ${item.name} - ${new Date(item.addedAt).toLocaleString()} - - `; - - favoritesList.appendChild(li); - }); - - // 削除ボタンにイベントリスナーを追加 - document.querySelectorAll('.delete-btn').forEach(button => { - button.addEventListener('click', (event) => { - const index = event.target.dataset.index; - deleteFavorite(index); - }); - }); - } - - // 削除する関数 - function deleteFavorite(index) { - favorites.splice(index, 1); // 該当のデータを削除 - localStorage.setItem('favorites', JSON.stringify(favorites)); // 更新されたデータを localStorage に保存 - renderList(favorites); // 再描画 - } - - // 初期表示(追加日時順) - renderList(favorites); -});