-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
26 lines (24 loc) · 834 Bytes
/
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
const form = document.querySelector('form');
const searchInput = document.querySelector('#search-input');
const API_KEY = '1';
if (form) {
form.addEventListener('submit', function (event) {
event.preventDefault();
const searchTerm = searchInput.value;
fetch(`https://www.themealdb.com/api/json/v1/${API_KEY}/search.php?s=${searchTerm}`, {
method: 'GET',
})
.then((response) => response.json())
.then((data) => {
if (data.meals && data.meals.length > 0) {
const queryString = '?results=' + encodeURIComponent(JSON.stringify(data.meals));
window.location.href = 'results.html' + queryString;
} else {
window.location.href = 'results.html?results=';
}
})
.catch((error) => {
console.error('Error:', error);
});
});
}