weather widget from lesson 9

Mateo Gomez 1 year ago
commit 37a91d6ebe2b6ef2c0f04286c0e03b33b7b408ec
2 changed files with 26 additions and 0 deletions
+16-0index.html ADDED
View File
@@ -0,0 +1,16 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+ <meta charset="utf-8">
5+ <title>Weather</title>
6+ <link rel="stylesheet" href="style.css">
7+</head>
8+<body>
9+ <form id="city-form">
10+ <input name="city" placeholder="City" required>
11+ <button>Go</button>
12+ </form>
13+ <p id="weather"></p>
14+ <script src="weather.js"></script>
15+</body>
16+</html>
+10-0weather.js ADDED
View File
@@ -0,0 +1,10 @@
1+const form = document.querySelector("#city-form");
2+const out = document.querySelector("#weather");
3+
4+form.addEventListener("submit", async e => {
5+ e.preventDefault();
6+ const geo = await (await fetch(`https://geocoding-api.open-meteo.com/v1/search?name=${form.city.value}&count=1`)).json();
7+ const place = geo.results[0];
8+ const w = await (await fetch(`https://api.open-meteo.com/v1/forecast?latitude=${place.latitude}&longitude=${place.longitude}&current=temperature_2m`)).json();
9+ out.textContent = `${place.name}: ${w.current.temperature_2m} °C`;
10+});