-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathforecast.js
48 lines (43 loc) · 1.65 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
$(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=52827ce67cdeeaaf0c60c4b244f1bf10',
type: "GET",
dataType: "jsonp",
success: function(data){
var table='';
var header='<h2 style="font-weight:bold;">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>");
}
}