-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (31 loc) · 1.16 KB
/
script.js
File metadata and controls
33 lines (31 loc) · 1.16 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
const API_KEY="c80821be1a2287cd32383ad92c1e3655"
const locatione = document.getElementById("locatione");
const temperature = document.getElementById("temperature");
const city = document.getElementById("city");
const p = document.getElementById("p");
p.style.visibility="hidden";
function searchCity() {
const cityName = city.value;
fetch('https://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&appid=' + API_KEY)
.then(function (resp) { return resp.json() })
.then(function (data) {
console.log(data);
if (!data.name) {
p.style.visibility = "visible";
}
else {
p.style.visibility="hidden";
locatione.innerText = data.name;
temperature.innerText = data.main.temp + "F / " + Math.round(parseFloat(data.main.temp) - 273.15) + "°C";
}
})
.catch(function (error) {
console.error('Error fetching data:', error);
});
}
const form = document.getElementById("search-form");
form.addEventListener("submit", function (event) {
event.preventDefault();
searchCity;
city.innerHTML = "";
});