mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-04 12:24:57 +00:00
Refactor button event handling to send direction via fetch to /send-direction
This commit is contained in:
@@ -1,23 +1,25 @@
|
||||
document.getElementById("form").addEventListener("submit", function(event) {
|
||||
event.preventDefault(); // voorkomt het herladen van de pagina
|
||||
// Selecteer alle knoppen en voeg een event listener toe aan elke knop
|
||||
document.querySelectorAll(".btn").forEach(button => {
|
||||
button.addEventListener("click", function(event) {
|
||||
event.preventDefault(); // voorkomt pagina-verversing
|
||||
|
||||
// Haal de waarde van de ingedrukte knop op
|
||||
const formData = new FormData(event.target);
|
||||
const direction = formData.get("direction"); // Haalt de 'direction' waarde op
|
||||
// Haal de waarde van de knop op
|
||||
const direction = event.target.value;
|
||||
|
||||
// Verstuur de richting via fetch naar de server
|
||||
fetch("/move", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({ direction: direction })
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
console.log("Success:", data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("Error:", error);
|
||||
// Verstuur de richting naar de server met fetch
|
||||
fetch("/send-direction", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({ direction: direction })
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
console.log("Success:", data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("Error:", error);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user