-
Notifications
You must be signed in to change notification settings - Fork 0
/
forecast.js
59 lines (41 loc) · 2.32 KB
/
forecast.js
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
$(document).ready(function(){
$("#submitForecast").click(function(){
return getForecast();
});
});
function getForecast(){
var city = $("#city").val();
var days = $("#days").val();
if(city != '' && days != ''){
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/forecast/daily?q=' + city + "&units=metric" + "&cnt=" + days + "&APPID=c10bb3bd22f90d636baa008b1529ee25",
type: "GET",
dataType: "jsonp",
success: function(data){
var table = '';
var header = '<h2 style="font-weight:bold; font-size:30px; margin-top:20px;">Weather forecast for ' + data.city.name + ', ' + data.city.country + '</h2>'
for(var i = 0; i < data.list.length; i++){
table += "<tr>";
table += "<td><img src='http://openweathermap.org/img/w/"+data.list[i].weather[0].icon+".png'></td>";
table += "<td>" + data.list[i].weather[0].main + "</td>";
table += "<td>" + data.list[i].weather[0].description + "</td>";
table += "<td>" + data.list[i].temp.morn + "°C</td>";
table += "<td>" + data.list[i].temp.night + "°C</td>";
table += "<td>" + data.list[i].temp.min + "°C</td>";
table += "<td>" + data.list[i].temp.max + "°C</td>";
table += "<td>" + data.list[i].pressure + "hpa</td>";
table += "<td>" + data.list[i].humidity + "%</td>";
table += "<td>" + data.list[i].speed + "m/s</td>";
table += "<td>" + data.list[i].deg + "°</td>";
table += "</tr>";
}
$("#forecastWeather").html(table);
$("#header").html(header);
$("#city").val('');
$("#days").val('')
}
});
}else{
$("#error").html("<div class='alert alert-danger' id='errorCity'><a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>City field cannot be empty</div>");
}
}