-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
29 lines (24 loc) · 1.08 KB
/
Copy pathscript.js
File metadata and controls
29 lines (24 loc) · 1.08 KB
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
async function getWeather() {
const city = document.getElementById("cityInput").value;
const apiKey = "YOUR_API_KEY"; // Replace with your OpenWeatherMap API key
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.cod !== 200) {
document.getElementById("weatherResult").innerHTML = "City not found!";
return;
}
const weatherHtml = `
<h2>${data.name}, ${data.sys.country}</h2>
<p><strong>${data.weather[0].main}</strong> - ${data.weather[0].description}</p>
<p>🌡️ Temperature: ${data.main.temp} °C</p>
<p>💧 Humidity: ${data.main.humidity}%</p>
<p>🌬️ Wind Speed: ${data.wind.speed} m/s</p>
`;
document.getElementById("weatherResult").innerHTML = weatherHtml;
} catch (error) {
document.getElementById("weatherResult").innerHTML =
"Error fetching weather!";
}
}