-
Notifications
You must be signed in to change notification settings - Fork 0
/
meal.js
81 lines (49 loc) · 1.89 KB
/
meal.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
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
const search = document.getElementById('search-button');
const mealList = document.getElementById('mealItems');
const mealDetails = document.getElementById('mealDetails');
const notFound = document.getElementById("not-found");
search.addEventListener('click', () => {
searchValue = document.getElementById('search-bar').value;
fetch(`https://www.themealdb.com/api/json/v1/1/search.php?s=${searchValue}`)
.then((res) => res.json())
.then(data => displayMeal(data.meals))
});
const displayMeal = (meals) => {
//---Resetting the values---
mealDetails.innerHTML = "";
notFound.innerHTML = "";
mealDetails.style.display = "none";
//---If meals not found---
if (meals == null) {
return showNotFound();
}
meals.forEach(meal => {
let mealDiv = document.createElement('div');
mealDiv.className = 'food';
mealDiv.innerHTML = `<img src=${meal.strMealThumb}
> <h3>${meal.strMeal}</h3>`;
mealList.appendChild(mealDiv);
//Meal Details about item
mealDiv.addEventListener('click', () => {
mealDetails.innerHTML = `
<img class="w-50 rounded" src=${meal.strMealThumb} alt=${meal.strMeal} class="card-img-top">
<div class="card-body">
<h5 class="card-title">${meal.strMeal}</h5>
<ul>
<li>${meal.strIngredient1}</li>
<li>${meal.strIngredient2}</li>
<li>${meal.strIngredient3}</li>
<li>${meal.strIngredient4}</li>
<li>${meal.strIngredient5}</li>
<li>${meal.strIngredient6}</li>
<li>${meal.strIngredient7}</li>
</ul>
</div>
</div>`;
window.scroll(0,0);
})
})
}
const showNotFound = () => {
notFound.innerHTML = `<h1 class="not-found">Items not found. Search again</h1>`;
}