forked from CareerDevelopmentHub/Beginner_WeatherAPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
35 lines (33 loc) · 1.15 KB
/
Copy pathscript.js
File metadata and controls
35 lines (33 loc) · 1.15 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
function getWeather() {
var city = document.getElementById("city").value;
fetch(
"http://api.openweathermap.org/data/2.5/weather?q=" +
city +
"&appid=your_api_key"
)
.then((response) => response.json())
.then((data) => {
var temp = Math.round(data.main.temp - 273.15);
var humidity = data.main.humidity;
var windSpeed = data.wind.speed;
var iconurl = "http://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
var m=data.weather[0].main;
var image = document.createElement("img");
image.setAttribute("id", "wicon");
image.setAttribute("src", iconurl);
image.setAttribute("alt", "Weather icon");
var showWeather = document.getElementById("showWeather");
var showCountry = document.getElementById("showCountry");
showCountry.innerHTML=city+"<br>";
showCountry.appendChild(image);
showWeather.innerHTML =
m+"<br>"+
temp +
"°C<br> Humidity is " +
humidity +
"% <br>Wind Speed is " +
windSpeed +
" m/s <br>";
})
.catch((err) => alert("City not found"));
}