-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
32 lines (28 loc) · 1.32 KB
/
script.js
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
const api = {
url: "https://api.openweathermap.org/data/2.5/weather?units=metric&q=",
key: "9fd3e42f107014b62cb7b2bbfcbea1bd"
};
const searchBox = document.querySelector(".search input");
const searchBtn = document.querySelector(".search button");
searchBtn.addEventListener("click", () => {
checkWeather(searchBox.value);
})
searchBox.addEventListener("keydown", (e) => {
if (e.keyCode == 13) {
checkWeather(searchBox.value);
}
});
async function checkWeather(city) {
const response = await fetch(api.url + city + `&appid=${api.key}`);
var data = await response.json();
console.log(data);
document.querySelector(".city").innerHTML = data.name + ", " + data.sys.country;
document.querySelector(".temp").innerHTML = Math.round(data.main.temp) + "°c";
document.querySelector(".tempMaxMin").innerHTML = Math.round(data.main.temp_min) + "°c/" + Math.round(data.main.temp_max) + "°c";
document.querySelector(".description").innerHTML = data.weather[0].main;
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = data.wind.speed + "km/h";
const icon = data.weather[0].icon;
const imageURL = "https://openweathermap.org/img/wn/" + icon + "@2x.png";
document.querySelector(".weatherIcon").src = imageURL;
};