-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
29 lines (23 loc) · 982 Bytes
/
Copy pathscript.js
File metadata and controls
29 lines (23 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const apiKey = "65fe78b69d4c23e7a72cc1a7175b7b48"; //valid OpenWeatherMap key
async function getWeather() {
const city = document.getElementById("cityInput").value.trim();
if (!city) {
alert("Please enter a city name");
return;
}
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.cod !== 200) {
throw new Error(data.message);
}
document.getElementById("cityName").innerText = data.name;
document.getElementById("temperature").innerText = `${data.main.temp} °C`;
document.getElementById("humidity").innerText = `${data.main.humidity} %`;
document.getElementById("description").innerText = data.weather[0].description;
document.getElementById("weatherInfo").classList.remove("hidden");
} catch (error) {
alert("Error: " + error.message);
}
}