-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (44 loc) · 1.77 KB
/
Copy pathapp.js
File metadata and controls
53 lines (44 loc) · 1.77 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
42
43
44
45
46
47
48
49
50
51
52
53
const ApiKey = "eefb18dac344907b648061a13f57de21";
const ApiUrl =
"https://api.openweathermap.org/data/2.5/weather?units=metric&q=";
const searchBox = document.querySelector(".search input");
const searchBtn = document.querySelector(".search button");
const weatherPic = document.querySelector(".weather_icon");
async function getWeather(city) {
const response = await fetch(ApiUrl + city + `&appid=${ApiKey}`);
if (response.status == 404) {
document.querySelector(".error").style.display = "block";
} else {
var data = await response.json();
console.log(data);
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".temp").innerHTML =
Math.round(data.main.temp) + "°C";
document.querySelector(".description").innerHTML =
data.weather[0].description;
document.querySelector(".humid").innerHTML = data.main.humidity + "%";
document.querySelector(".speed").innerHTML = data.wind.speed + " km/h";
if (data.weather[0].main == "Clouds") {
weatherPic.src = "./images/clouds.png";
} else if (data.weather[0].main == "Clear") {
weatherPic.src = "./images/clear.png";
} else if (data.weather[0].main == "Mist") {
weatherPic.src = "./images/mist.png";
} else if (data.weather[0].main == "Rain") {
weatherPic.src = "./images/rain.png";
} else if (data.weather[0].main == "Snow") {
weatherPic.src = "./images/snow.png";
} else if (data.weather[0].main == "Drizzle") {
weatherPic.src = "./images/drizzle.png";
}
document.querySelector(".error").style.display = "none";
}
}
searchBtn.addEventListener("click", () => {
getWeather(searchBox.value);
});
searchBox.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
getWeather(searchBox.value);
}
});