-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
45 lines (40 loc) · 1.39 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
let show = document.getElementById("show");
let search = document.getElementById("search");
let cityVal = document.getElementById("city");
let key = "abfb95a9a3ea6e90704c8b5c78be3c84";
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);