-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
44 lines (42 loc) · 1.52 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
33
34
35
36
37
38
39
40
41
42
43
44
/* --------------- Weather Web App --------------------- */
let show = document.getElementById("show");
let search = document.getElementById("search");
let cityVal = document.getElementById("city");
//Make sure you have your own key.
let key = "2f745fa85d563da5adb87b6cd4b81caf";
let getWeather = () => {
let cityValue = cityVal.value;
if (cityValue.length == 0) {
show.innerHTML = `<h3 class="error">Please enter a city name</h3>`;
}
else {
let url = `https://api.openweathermap.org/data/2.5/weather?q=${cityValue}&appid=${key}&units=metric`;
cityVal.value = "";
fetch(url)
.then((resp) => resp.json())
.then((data) => {
show.innerHTML = `
<h2>${data.name}, ${data.sys.country}</h2>
<h4 class="weather">${data.weather[0].main}</h4>
<h4 class="desc">${data.weather[0].description}</h4>
<img src="https://openweathermap.org/img/w/${data.weather[0].icon}.png">
<h1>${data.main.temp} °</h1>
<div class="temp_container">
<div>
<h4 class="title">min</h4>
<h4 class="temp">${data.main.temp_min}°</h4>
</div>
<div>
<h4 class="title">max</h4>
<h4 class="temp">${data.main.temp_max}°</h4>
</div>
</div>
`;
})
.catch(() => {
show.innerHTML = `<h3 class="error">City not found</h3>`;
});
}
};
search.addEventListener("click", getWeather);
window.addEventListener("load", getWeather);