-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (63 loc) · 2.28 KB
/
Copy pathscript.js
File metadata and controls
81 lines (63 loc) · 2.28 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
75
76
77
78
79
80
81
const input = document.getElementById("searchbox");
const searchbtn = document.getElementById("searchbtn");
const title = document.getElementById("title");
const closeBtn = document.getElementById("closeBtn");
const RecipeItem = document.querySelector(".recipe-item")
const RecipeDetails = document.querySelector(".recipe-deatils")
const recipeContainer = document.getElementById("recipr-container")
let recipe ;
searchbtn.addEventListener("click", (e) => {
e.preventDefault();
recipe = input.value;
getrecipe()
});
async function getrecipe() {
title.innerText = `Fetching ${recipe} Recipe...`
const API_URL = `https://www.themealdb.com/api/json/v1/1/search.php?s=${recipe}`;
const res = await fetch(API_URL)
const data = await res.json()
console.log(data.meals)
data.meals.forEach(meal => {
const ReciprDiv = document.createElement("div")
ReciprDiv.classList.add("recipediv")
ReciprDiv.innerHTML = `
<img src="${meal.strMealThumb}"/>
<h3>${meal.strMeal}</h3>
<p>Country : ${meal.strArea}</p>
<p>Category : ${meal.strCategory}</p>
`
const button = document.createElement("button")
button.textContent = "Veiw Details"
recipeContainer.appendChild(ReciprDiv)
ReciprDiv.appendChild(button)
title.innerText = `All ${recipe} Recipe`
button.addEventListener("click", () => {
RecipeDetails.style.opacity = 1;
RecipeItem.innerHTML = `<h2>${meal.strMeal}</h2>
<h3>Ingredents</h3>
<ul>${getItem(meal)}</ul>
<div>
<h3>Instructions:</h3>
<p>${meal.strInstructions}</p>
</div>
`
})
});
closeBtn.addEventListener("click", () => {
RecipeDetails.style.opacity = 0;
})
const getItem = (meal) => {
console.log(meal)
let IngredentsList = "";
for (let i = 1; i <= 20; i++) {
const Ingredents = meal[`strIngredient${i}`]
if(Ingredents){
const measure = meal[`strMeasure${i}`]
IngredentsList += `<li>${measure} ${Ingredents}</li>`
}else{
break
}
}
return IngredentsList
}
}