main weather/weather.js

11 lines626 B

const form = document.querySelector("#city-form");
const out = document.querySelector("#weather");
form.addEventListener("submit", async e => {
e.preventDefault();
const geo = await (await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${form.city.value}&count=1`)).json();
if (!geo.results) { out.textContent = "City not found"; return; }
const place = geo.results[0];
const w = await (await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${place.latitude}&longitude=${place.longitude}&current=temperature_2m`)).json();
out.textContent = `${place.name}: ${w.current.temperature_2m} °C`;
});