weather widget from lesson 9
commit 3072243ccece23222990de4e2b572886fcb6a99b
2 changed files with 26 additions and 0 deletions
index.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> | |
weather.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}¤t=temperature_2m`)).json(); | |
| 9 | + out.textContent = `${place.name}: ${w.current.temperature_2m} °C`; | |
| 10 | +}); | |