-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (34 loc) · 1.17 KB
/
script.js
File metadata and controls
40 lines (34 loc) · 1.17 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
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('recipe-form');
const clearButton = document.getElementById('clearButton');
const recipeOutput = document.getElementById('recipeOutput');
form.addEventListener('submit', (e) => {
e.preventDefault();
generateRecipe();
});
clearButton.addEventListener('click', () => {
document.getElementById('ingredientInput').value = '';
recipeOutput.innerHTML = '';
});
});
function generateRecipe() {
const ingredientInput = document.getElementById('ingredientInput');
const recipeOutput = document.getElementById('recipeOutput');
const ingredients = ingredientInput.value;
// Send a request to the backend with the ingredients
fetch('/generate_recipe', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ ingredients: ingredients })
})
.then(response => response.json())
.then(data => {
recipeOutput.textContent = data.recipe;
})
.catch(error => {
console.error('Error:', error);
recipeOutput.textContent = 'An error occurred.';
});
}