-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
74 lines (63 loc) · 2.76 KB
/
script.js
File metadata and controls
74 lines (63 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const recipeForm = document.getElementById('recipeForm');
const recipeList = document.getElementById('recipeList');
const searchInput = document.getElementById('searchInput');
let recipes = JSON.parse(localStorage.getItem('recipes')) || [];
function displayRecipes() {
recipeList.innerHTML = '';
recipes.forEach((recipe, index) => {
const li = document.createElement('li');
li.innerHTML = `
<h3>${recipe.name} (${recipe.category})</h3>
<p><strong>Ingredients:</strong> ${recipe.ingredients}</p>
<p><strong>Instructions:</strong> ${recipe.instructions}</p>
<button onclick="editRecipe(${index})">Edit</button>
<button onclick="deleteRecipe(${index})">Delete</button>
`;
recipeList.appendChild(li);
});
}
function deleteRecipe(index) {
recipes.splice(index, 1);
localStorage.setItem('recipes', JSON.stringify(recipes));
displayRecipes();
}
function editRecipe(index) {
const recipe = recipes[index];
document.getElementById('recipeName').value = recipe.name;
document.getElementById('recipeCategory').value = recipe.category;
document.getElementById('ingredients').value = recipe.ingredients;
document.getElementById('instructions').value = recipe.instructions;
// Remove the recipe from the list to avoid duplication
deleteRecipe(index);
}
recipeForm.addEventListener('submit', (event) => {
event.preventDefault();
const name = document.getElementById('recipeName').value;
const category = document.getElementById('recipeCategory').value;
const ingredients = document.getElementById('ingredients').value;
const instructions = document.getElementById('instructions').value;
recipes.push({ name, category, ingredients, instructions });
localStorage.setItem('recipes', JSON.stringify(recipes));
displayRecipes();
recipeForm.reset();
});
function filterRecipes() {
const searchTerm = searchInput.value.toLowerCase();
const filteredRecipes = recipes.filter(recipe =>
recipe.name.toLowerCase().includes(searchTerm) ||
recipe.ingredients.toLowerCase().includes(searchTerm)
);
recipeList.innerHTML = '';
filteredRecipes.forEach((recipe, index) => {
const li = document.createElement('li');
li.innerHTML = `
<h3>${recipe.name} (${recipe.category})</h3>
<p><strong>Ingredients:</strong> ${recipe.ingredients}</p>
<p><strong>Instructions:</strong> ${recipe.instructions}</p>
<button onclick="editRecipe(${index})">Edit</button>
<button onclick="deleteRecipe(${index})">Delete</button>
`;
recipeList.appendChild(li);
});
}
displayRecipes();