-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (24 loc) · 874 Bytes
/
Copy pathscript.js
File metadata and controls
31 lines (24 loc) · 874 Bytes
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
const url = 'https://api.openweathermap.org/data/2.5/';
const key = 'YOUR_OPENWEATHERMAP_API_KEY';
const setQuery = (e) => {
if (e.keyCode == '13')
getResult(searchBar.value)
}
const getResult = (cityName) => {
let query = `${url}weather?q=${cityName}&appid=${key}&units=metric&lang=en`
fetch(query)
.then(weather => {
return weather.json()
})
.then(displayResult)
}
const displayResult = (result) => {
let city = document.querySelector('.city')
city.innerText = `${result.name}, ${result.sys.country}`
let temp = document.querySelector('.temp')
temp.innerText = `${Math.round(result.main.temp)}°C `
let desc = document.querySelector('.desc')
desc.innerText = result.weather[0].description
}
const searchBar = document.getElementById('searchBar')
searchBar.addEventListener('keypress' ,setQuery)