-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (41 loc) · 1.43 KB
/
index.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
var apiKey = "45cd964728f300c81057241ce6ee826d";
var searchBtn = $("#search-btn");
var searchInput = $("#city")
searchBtn.on("click", function(e){
e.preventDefault();
var cityName = $("input").val().toUpperCase().trim();
searchInput.val("");
getWeather(cityName);
});
$(document).on("click", function() {
var cityName = $(this).text();
getWeather(cityName);
});
function displayCurrentWeather(){
var cardDiv = $("<div class='container border bg-light'>");
var header = $("<h3>").text(city + " " + date.toString());
var image = $("<img>").attr("src", weatherIconUrl);
header.append(image);
var temperature = $("<p>").text("Temperature: " + temp+ " ºF");
cardDiv.append(header);
cardDiv.append(temperature);
$("#current-weather").append(cardDiv)
}
function getWeather(cityName) {
var queryURL = "https://api.openweathermap.org/data/2.5/weather?q=" +
cityName + "&appid=" + apiKey;
$.ajax({
url: queryURL,
method: "GET"
}).then(function(response) {
var result = response;
console.log(result);
city = result.name.trim();
date = moment.unix(result.dt).format("l");
var tempK = result.main.temp;
temp = ((tempK - 273.15) * 1.80 + 32).toFixed(1);
weatherIcon = result.weather[0].icon;
weatherIconUrl = "https://openweathermap.org/img/w/" + weatherIcon + ".png";
displayCurrentWeather()
})
};