-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
65 lines (59 loc) · 2.15 KB
/
main.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const KEY = "f302d1cceaa3b48035dff6cbbbea7c7a";
const URL = "https://api.openweathermap.org/data/2.5/weather?units=metric&q=";
const searchInput = document.querySelector(".seach-box input");
const searchBtn = document.querySelector(".seach-box button");
const weather = document.querySelector(".weather");
const error = document.querySelector(".error");
async function searchWeather(cityName) {
const response = await fetch(URL + cityName + `&appid=${KEY}`);
if (response.status == 404) {
error.style.display = "block";
weather.style.display = "none";
}
const data = await response.json();
document.querySelector(".weather").innerHTML = `
<div class="weather-image">
<i class="fa-solid fa-cloud"></i>
</div>
<h1 class="temperature">${Math.round(data.main.temp)}℃</h1>
<h2 class="city">${data.name}</h2>
<ul class="details">
<li class="colon">
<i class="fa-solid fa-water"></i>
<div>
<p class="humidity">${data.main.humidity}%</p>
<p>Humidity</p>
</div>
</li>
<li class="colon">
<i class="fa-solid fa-wind"></i>
<div>
<p class="wind-speed">${data.wind.speed} km/h</p>
<p>Wind speed</p>
</div>
</li>
</ul>`;
const weatherIcon = document.querySelector(".weather-image i");
if (data.weather[0].main == "Clear") {
weatherIcon.className = "fa-solid fa-sun";
} else if (data.weather[0].main == "Rain") {
weatherIcon.className = "fa-solid fa-cloud-rain";
} else if (data.weather[0].main == "Mist") {
weatherIcon.className = "fa-solid fa-cloud-mist";
} else if (data.weather[0].main == "Drizzle") {
weatherIcon.className = "fa-solid fa-cloud-drizzle";
}
error.style.display = "none";
weather.style.display = "block";
}
searchBtn.addEventListener("click", (event) => {
event.preventDefault();
searchWeather(searchInput.value);
searchInput.value = "";
});
searchInput.addEventListener("keydown", (event) => {
if (event.keyCode === 13) {
searchWeather(searchInput.value);
searchInput.value = "";
}
});