-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
67 lines (58 loc) · 1.98 KB
/
script.js
File metadata and controls
67 lines (58 loc) · 1.98 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// api keys, base url, and other variable with nuance value
const key = "92d6e5bcc79fe1ff7f4e642716c2134a";
const base_url = "https://api.openweathermap.org/data/2.5/weather?";
const search = document.querySelector(".search-icon-border");
let city;
let api_url;
// click function to dynamically changing the url
search.addEventListener("click", () => {
city = document.querySelector("#search_input").value.trim();
if (city) {
api_url = `${base_url}q=${city}&appid=${key}`;
// click function to dynamically changing the url
api_fetch();
} else {
alert("please enter the city name");
}
});
const search_input = document.getElementById("search_input");
search_input.addEventListener("keydown", (event) => {
city = document.querySelector("#search_input").value.trim();
if (event.key === "Enter") {
api_url = `${base_url}q=${city}&appid=${key}`;
if (city) {
api_fetch();
console.log("tha's it");
} else {
alert("enter city name");
}
}
});
const api_fetch = () => {
fetch(api_url)
.then((res) => res.json())
.then((data) => {
// city name
document.querySelector(".cityName").innerText = data.name;
// icon
const icon_code = data.weather[0].icon;
const icon_url = `http://openweathermap.org/img/wn/${icon_code}@2x.png`;
document.querySelector("#img").src = icon_url;
// temperature
document.querySelector(".weather-logo p").innerText =
data.weather[0].description;
const temp = Math.trunc(data.main.temp - 273.15);
document.querySelector(".weather-logo h1").innerText = `${temp} C`;
// weather details
document.querySelector(
"#humidity"
).innerText = `Humidity: ${data.main.humidity}`;
document.querySelector(
"#pressure"
).innerText = `Pressure: ${data.main.pressure}`;
document.querySelector("#wind").innerText = `Wind: ${data.wind.speed}`;
})
.catch((error) => {
console.log("not able to fetch data");
});
};