-
Notifications
You must be signed in to change notification settings - Fork 12
/
script.js
43 lines (39 loc) · 1.77 KB
/
script.js
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
window.onload = function () {
const urlParams = new URLSearchParams(window.location.search);
const recipeId = urlParams.get("id");
fetch("recipes.json")
.then((response) => response.json())
.then((recipes) => {
const recipe = recipes.find((r) => r.id === recipeId);
if (recipe) {
document.getElementById("recipe-name").innerText = recipe.name.toUpperCase();
document.getElementById("recipe-image").src = recipe.image;
document.getElementById("recipe-image").height = 600;
document.getElementById("recipe-image").width = 400;
document.getElementById("recipe-image").alt = recipe.name;
document.getElementById("recipe-description").innerText = recipe.description;
document.getElementById("recipe-time").innerText = recipe.time;
document.getElementById("recipe-servings").innerText = recipe.servings;
document.getElementById("recipe-difficulty").innerText = recipe.difficulty;
const ingredientsList = document.getElementById("ingredients-list");
recipe.ingredients.forEach((ingredient) => {
const li = document.createElement("li");
li.innerText = ingredient;
ingredientsList.appendChild(li);
});
const instructionsList = document.getElementById("instructions-list");
instructionsList.style.listStyleType = "none";
instructionsList.style.paddingLeft = "0";
recipe.instructions.forEach((instruction) => {
const li = document.createElement("li");
li.innerHTML = `${instruction}<br><br>`;
instructionsList.appendChild(li);
});
} else {
alert("Recipe not found!");
}
})
.catch((error) => {
console.error("Error fetching the recipe data:", error);
});
};