-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (61 loc) · 2.68 KB
/
app.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
function getData() {
const date = new Date();
let currentDate = `${date}`;
const getHeader = document.getElementById('header');
const getLocation = document.getElementById('location');
const getCurrent = document.getElementById('current');
const API_KEY = 'de404b5787706a2e41c10f612aac0485';
const searchInput = document.createElement('input');
searchInput.setAttribute('type', 'text');
searchInput.setAttribute('autocomplete', 'on');
searchInput.setAttribute('id', 'search-input');
searchInput.setAttribute('placeholder', 'Enter a City (Please check for spelling errors)');
const searchButton = document.createElement('button');
searchButton.setAttribute('id', 'search-Button');
searchButton.innerText = '🔍';
getHeader.append(searchInput);
getHeader.append(searchButton);
searchButton.addEventListener('click', fetchData);
searchInput.addEventListener('keyup', function (event) {
if (event.key === 'Enter') {
fetchData();
}
});
function fetchData() {
const city = searchInput.value;
const API_URL = `https://api.openweathermap.org/data/2.5/weather?q=${city}&APPID=${API_KEY}`;
fetch(API_URL)
.then((response) => response.json())
.then((data) => {
const result = data;
const createLocation = document.createElement('div');
createLocation.setAttribute('class', 'city');
createLocation.innerText = result.name + ', ' + result.sys.country;
const createDate = document.createElement('div');
createDate.setAttribute('class', 'date');
createDate.innerText = currentDate.slice(0, 15);
const createTemp = document.createElement('div');
createTemp.setAttribute('class', 'temp');
createTemp.innerText = result.main.temp + '°c';
const createWeather = document.createElement('div');
createWeather.setAttribute('class', 'weather');
createWeather.innerText = result.weather[0].main;
const createMinMaxTemp = document.createElement('div');
createMinMaxTemp.setAttribute('class', 'hi-low');
createMinMaxTemp.innerText =
result.main.temp_max + '°c' + ' / ' + result.main.temp_min + '°c';
while (getLocation.firstChild) {
getLocation.removeChild(getLocation.firstChild);
}
while (getCurrent.firstChild) {
getCurrent.removeChild(getCurrent.firstChild);
}
getLocation.append(createLocation);
getLocation.append(createDate);
getCurrent.append(createTemp);
getCurrent.append(createWeather);
getCurrent.append(createMinMaxTemp);
});
}
}
getData();