-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
41 lines (33 loc) · 1.53 KB
/
script.js
File metadata and controls
41 lines (33 loc) · 1.53 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
30
31
32
33
34
35
36
37
38
39
40
41
const apiKey = "5dd03b2987680039ace633846d734f0b";
const apiUrl = "https://api.openweathermap.org/data/2.5/weather?units=metric&q=";
const searchBox = document.querySelector("#city-input");
const searchBtn = document.querySelector("#search-btn");
const weatherIcon = document.querySelector("#weather-icon");
const display = document.querySelector("#weather-display");
const errorMsg = document.querySelector("#error-msg");
async function checkWeather(city) {
const response = await fetch(apiUrl + city + `&appid=${apiKey}`);
if (response.status == 404) {
errorMsg.style.display = "block";
display.style.display = "none";
} else {
const data = await response.json();
document.querySelector("#city").innerHTML = data.name;
document.querySelector("#temp").innerHTML = Math.round(data.main.temp) + "°C";
document.querySelector("#humidity").innerHTML = data.main.humidity + "%";
document.querySelector("#wind").innerHTML = data.wind.speed + " km/h";
// Dynamic Icon Logic
const condition = data.weather[0].main;
const iconCode = data.weather[0].icon;
// Use OpenWeather's own icons for simplicity
weatherIcon.src = `https://openweathermap.org/img/wn/${iconCode}@2x.png`;
display.style.display = "block";
errorMsg.style.display = "none";
}
}
searchBtn.addEventListener("click", () => {
checkWeather(searchBox.value);
});
searchBox.addEventListener("keypress", (e) => {
if (e.key === "Enter") checkWeather(searchBox.value);
});