Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (40 loc) · 1.42 KB
/
Copy pathscript.js
File metadata and controls
46 lines (40 loc) · 1.42 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
const submitBtn = document.querySelector('.fa-search');
const searchInput = document.querySelector('.search-input');
const resultsList = document.querySelector('#results');
submitBtn.addEventListener('click', handleSearch);
async function handleSearch(e) {
e.preventDefault();
const searchValue = searchInput.value.trim();
const recipes = await fetchRecipes(searchValue);
displayRecipes(recipes);
}
async function fetchRecipes(searchValue) {
const API_URL = `https://api.edamam.com/search`;
const APP_ID = '7aa516a5';
const APP_KEY = 'dc836a223fb788b11ae390504d9e97ce';
const requestURL = `${API_URL}?q=${searchValue}&app_id=${APP_ID}&app_key=${APP_KEY}&from=0&to=10`;
try {
const response = await fetch(requestURL);
const data = await response.json();
return data.hits;
} catch (error) {
console.error('Error fetching recipes:', error);
return [];
}
}
function displayRecipes(recipes) {
let html = '';
recipes.forEach(recipe => {
const { image, label, ingredientLines, url } = recipe.recipe;
html += `
<div>
<img src="${image}" alt="${label}">
<h3>${label}</h3>
<ul>
${ingredientLines.map(ingredient => `<li>${ingredient}</li>`).join('')}
</ul>
<a href="${url}" target="_blank">View Recipe</a>
</div>`;
});
resultsList.innerHTML = html;
}