Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (84 loc) · 2.71 KB
/
Copy pathscript.js
File metadata and controls
139 lines (84 loc) · 2.71 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const apiKey='9be4155d55710f4d322b2bebc1302cfc';
const searchBtn = document.getElementById('searchBtn');
if (searchBtn) {
searchBtn.addEventListener('click', getWeather);
}
function getWeather(){
const city=document.getElementById('cityInput').value;
if (city==''){
alert("Enter the city name");
return;
}
const url =
`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
fetch(url)
.then(response => response.json())
.then(data => {
document.getElementById("cityName").innerText = data.name;
document.getElementById("temperature").innerText =
Math.round(data.main.temp) + "°C";
document.getElementById("description").innerText =
data.weather[0].description;
document.getElementById("feelsLike").innerText =
Math.round(data.main.feels_like) + "°C";
document.getElementById("humidity").innerText =
data.main.humidity + "%";
document.getElementById("wind").innerText =
data.wind.speed + " m/s";
})
.catch(error => {
alert("City not found");
});
}
// forecast script
const forecastBtn = document.getElementById("forecastBtn");
if(forecastBtn){
forecastBtn.addEventListener("click", getForecast);
}
function getForecast(){
const city = document.getElementById("forecastCity").value;
const url =
`https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${apiKey}&units=metric`;
fetch(url)
.then(res => res.json())
.then(data => {
const container = document.getElementById("forecastContainer");
container.innerHTML = "";
data.list.slice(0,5).forEach(item => {
const card = document.createElement("div");
card.classList.add("forecast-card");
const date = new Date(item.dt_txt);
card.innerHTML = `
<p>${date.toDateString()}</p>
<h3>${Math.round(item.main.temp)}°C</h3>
<p>${item.weather[0].main}</p>
`;
container.appendChild(card);
});
});
}
// save page
const input = document.getElementById("saveCityInput");
const btn = document.getElementById("saveCityBtn");
const list = document.getElementById("cityList");
btn.addEventListener("click", function(){
const city = input.value;
if(city === ""){
alert("Enter city");
return;
}
let cities = JSON.parse(localStorage.getItem("cities")) || [];
cities.push(city);
localStorage.setItem("cities", JSON.stringify(cities));
showCities();
});
function showCities(){
let cities = JSON.parse(localStorage.getItem("cities")) || [];
list.innerHTML = "";
cities.forEach(function(city){
const li = document.createElement("li");
li.textContent = city;
list.appendChild(li);
});
}
showCities();