Refactor button handling in index.html and app.py

This commit is contained in:
ishak jmilou.ishak
2024-10-24 12:14:52 +02:00
parent 4f3bb0b009
commit 5fe14a2f8f
3 changed files with 60 additions and 26 deletions

View File

@@ -0,0 +1,19 @@
document.querySelectorAll('button').forEach(button => {
button.addEventListener('click', function() {
const direction = this.value;
fetch('/move', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({ direction: direction }),
})
.then(response => response.json())
.then(data => {
document.getElementById('response').innerText = data.message;
})
.catch(error => {
document.getElementById('response').innerText = 'Er is een fout opgetreden: ' + error;
});
});
});