Fix search bar
This commit is contained in:
parent
e74baed746
commit
887a167422
@ -15,10 +15,10 @@
|
|||||||
<h1>Postnummer App</h1>
|
<h1>Postnummer App</h1>
|
||||||
</div>
|
</div>
|
||||||
<main>
|
<main>
|
||||||
<div id="search-bar">
|
<form id="search-bar">
|
||||||
<input id="search-input" placeholder="Postnummer">
|
<input id="search-input" type="text" placeholder="Postnummer" maxlength="4">
|
||||||
<button id="search-button">Search</button>
|
<button id="search-button" type="submit">Search</button>
|
||||||
</div>
|
</form>
|
||||||
<img src="assets/map.jpg" id="map"><br>
|
<img src="assets/map.jpg" id="map"><br>
|
||||||
<p id="mouse-position"></p>
|
<p id="mouse-position"></p>
|
||||||
<p id="coords"></p>
|
<p id="coords"></p>
|
||||||
|
@ -96,12 +96,18 @@ function setupMap(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setupSearchBar(zipCodeElement: HTMLParagraphElement) {
|
function setupSearchBar(zipCodeElement: HTMLParagraphElement) {
|
||||||
|
const searchBar =
|
||||||
|
document.querySelector<HTMLFormElement>("#search-bar");
|
||||||
const searchInput =
|
const searchInput =
|
||||||
document.querySelector<HTMLInputElement>("#search-input")!;
|
document.querySelector<HTMLInputElement>("#search-input")!;
|
||||||
const searchButton =
|
|
||||||
document.querySelector<HTMLButtonElement>("#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;
|
const inputValue = searchInput.value;
|
||||||
if (!/^\d+$/.test(inputValue)) return;
|
if (!/^\d+$/.test(inputValue)) return;
|
||||||
const data = await (
|
const data = await (
|
||||||
@ -109,11 +115,13 @@ function setupSearchBar(zipCodeElement: HTMLParagraphElement) {
|
|||||||
`https://api.dataforsyningen.dk/postnumre?nr=${inputValue}`,
|
`https://api.dataforsyningen.dk/postnumre?nr=${inputValue}`,
|
||||||
)
|
)
|
||||||
).json();
|
).json();
|
||||||
|
|
||||||
displayZipCode(
|
displayZipCode(
|
||||||
zipCodeElement,
|
zipCodeElement,
|
||||||
parseInt(data[0]["nr"]),
|
data.length ? parseInt(data[0]["nr"]) : null,
|
||||||
data[0]["navn"],
|
data.length ? data[0]["navn"] : null,
|
||||||
);
|
);
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user