-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (42 loc) · 1.67 KB
/
Copy pathscript.js
File metadata and controls
43 lines (42 loc) · 1.67 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
const weatherApp = {
apiKey: "d1845658f92b31c64bd94f06f7188c9c",
getWeather: function (city) {
fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${this.apiKey}`
)
.then((response) => response.json())
.then((data) => {
this.displayWeather(data);
})
.catch((error) => {
console.error("Error fetching data:", error);
});
},
displayWeather: function (data) {
const cityElement = document.querySelector(".city");
const tempElement = document.querySelector(".temp");
const wind = document.querySelector(".humidity");
const dis = document.querySelector(".description");
const op = document.querySelector(".open");
const card = document.querySelector(".card");
const lat = document.querySelector(".wind");
cityElement.textContent = `Weather of ${data.name}`;
tempElement.textContent = `${data.main.temp}°C`;
wind.textContent = `Wind speed is: ${data.wind.speed} km/h`;
dis.textContent = `${data.weather[0].main}`;
op.textContent = "Weather Report ";
lat.textContent = `Latitude: ${data.coord.lat} and Longitude: ${data.coord.lon}`;
document.body.style.backgroundImage =
"url('https://source.unsplash.com/1600x900/?" + data.name + "')";
},
};
document.querySelector(".search button").addEventListener("click", function () {
const cityName = document.querySelector(".search input").value;
weatherApp.getWeather(cityName);
});
document.querySelector(".searchbar").addEventListener("keyup", function (e) {
if (e.key === "Enter") {
const cityName = document.querySelector(".search input").value;
weatherApp.getWeather(cityName);
}
});