-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
50 lines (50 loc) · 2.09 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
44
45
46
47
48
49
50
//Add Enter button functionality
document
.getElementById("search-field")
.addEventListener("keypress", function (event) {
if (event.key == "Enter") {
document.getElementById("search-btn").click();
}
});
//Call the api
const searchItems = () => {
const searchField = document.getElementById("search-field").value;
const url = `https://www.themealdb.com/api/json/v1/1/search.php?f=${searchField}`;
fetch(url)
.then((response) => response.json())
.then((data) => showItems(data.meals));
};
//Showing Items
const showItems = (items) => {
const foodsContainer = document.getElementById("food-container");
foodsContainer.innerHTML = "";
items.forEach((singleItem) => {
const foodsDiv = document.createElement("div");
foodsDiv.className = "food-items";
foodsDiv.innerHTML =
`<div onclick="getItemDetails('${items[0].strMeal}');" class='food-box'>
<div class='food-icon'><img src='${singleItem.strMealThumb}'></div>
<div class='food-name'><h2>${singleItem.strMeal}</h2></div>`;
foodsContainer.appendChild(foodsDiv);
// getItemDetails(items[0].strMeal);
console.log(items);
});
};
//Item Details
const getItemDetails = async (name) => {
const url = `https://www.themealdb.com/api/json/v1/1/search.php?s=${name}`;
const res = await fetch(url);
const data = await res.json();
showItemDetails(data);
};
//show item details
const showItemDetails = (details) => {
console.log(details);
const foodDetails = document.getElementById("food-details");
const detailsDiv = document.createElement("div");
detailsDiv.innerHTML = `<img src="${details.meals[0].strMealThumb}">
<div class="details-name">${details.meals[0].strMeal}</div>
<div class="details-ingredient">${details.meals[0].strIngredient1}</div>
<div class="details-ingredient">${details.meals[0].strIngredient10}</div>`;
foodDetails.appendChild(detailsDiv);
};