-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
87 lines (67 loc) · 2.77 KB
/
script.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function getWeather(event) {
//event.preventDefault()
var search = document.getElementById("searchBox").value;
if(search === "") {
alert("Error! Please re-try search.");
} else if(isNaN(search)) {
var city = `https://api.openweathermap.org/data/2.5/weather?q=${search}&appid=${'08dac40a01916c37878befa9615d7b6b'}&units=imperial`;
fetch(city)
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data);
var cityOutput = document.getElementById("city");
var city = data.name;
cityOutput.innerHTML = city;
var zipOutput = document.getElementById("zip");
var zip = search;
zipOutput.innerHTML = "N/A";
var tempOutput = document.getElementById("temp");
var temp = data.main.temp;
tempOutput.innerHTML = temp;
var icon = "http://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
document.getElementById("graphic").src = icon;
})
.catch(function(err) {
console.log(err);
});
} else {
var zip = `https://api.openweathermap.org/data/2.5/weather?zip=${search}&appid=${'08dac40a01916c37878befa9615d7b6b'}&units=imperial`;
fetch(zip)
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data);
var zipOutput = document.getElementById("zip");
var zip = search;
zipOutput.innerHTML = zip;
var cityOutput = document.getElementById("city");
var city = data.name;
cityOutput.innerHTML = city;
var tempOutput = document.getElementById("temp");
var temp = data.main.temp;
tempOutput.innerHTML = temp;
var icon = "http://openweathermap.org/img/w/" + data.weather[0].icon + ".png";
document.getElementById("graphic").src = icon;
})
.catch(function(err) {
console.log(err);
});
}
}
var resetBtn = document.getElementById("resetBtn");
resetBtn.addEventListener("click", function(e) {
e.preventDefault();
console.log('test');
var search = document.getElementById("searchBox").value;
search = ""; //? won't clear text box!
var tempOutput = document.getElementById("temp");
tempOutput.innerHTML = "";
var zipOutput = document.getElementById("zip");
zipOutput.innerHTML = "";
var cityOutput = document.getElementById("city");
cityOutput.innerHTML = "";
document.getElementById("graphic").src = "";
})