diff --git a/frontend/index.html b/frontend/index.html
index 0631f40..2ececee 100644
--- a/frontend/index.html
+++ b/frontend/index.html
@@ -15,10 +15,10 @@
Postnummer App
-
-
-
-
+
diff --git a/frontend/src/main.ts b/frontend/src/main.ts
index e96c5f8..2ac4f71 100644
--- a/frontend/src/main.ts
+++ b/frontend/src/main.ts
@@ -96,12 +96,18 @@ function setupMap(
}
function setupSearchBar(zipCodeElement: HTMLParagraphElement) {
+ const searchBar =
+ document.querySelector("#search-bar");
const searchInput =
document.querySelector("#search-input")!;
- const searchButton =
- document.querySelector("#search-button")!;
- searchButton.onclick = async (_event: MouseEvent) => {
+ // Prevent typing letters
+ searchBar.onkeypress = (event: KeyboardEvent) => {console.log(event);
+ event.key !== "Enter" || !isNaN(parseInt(event.key));}
+
+ searchBar.onsubmit = async (event: MouseEvent) => {
+ event.preventDefault();
+
const inputValue = searchInput.value;
if (!/^\d+$/.test(inputValue)) return;
const data = await (
@@ -109,11 +115,13 @@ function setupSearchBar(zipCodeElement: HTMLParagraphElement) {
`https://api.dataforsyningen.dk/postnumre?nr=${inputValue}`,
)
).json();
+
displayZipCode(
zipCodeElement,
- parseInt(data[0]["nr"]),
- data[0]["navn"],
+ data.length ? parseInt(data[0]["nr"]) : null,
+ data.length ? data[0]["navn"] : null,
);
+
};
}