-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
31 lines (27 loc) · 1.03 KB
/
weather.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
let cityOutput = document.querySelector('[city]');
let tempOutput = document.querySelector('[temp]');
const weather_input = document.querySelector('[weather-input]');
weather_input.addEventListener('submit', function (e) {
e.preventDefault();
let location =document.querySelector('[gross-input]').value;
updateWeather(location);
});
async function updateWeather(city) {
cityOutput.innerHTML='<div id="preloader"><div id="loader"></div></div>';
tempOutput.innerHTML='';
let cityname = city;
const options_weather = {
method: 'GET',
headers: {
'X-RapidAPI-Key': '0fafe3de48mshce9c7b8d2fa9c1ap194196jsn2e9237f5a2c8',
'X-RapidAPI-Host': 'weather-by-api-ninjas.p.rapidapi.com'
}
};
await fetch('https://weather-by-api-ninjas.p.rapidapi.com/v1/weather?city=' + cityname, options_weather)
.then(response => response.json())
.then((response) =>{
tempOutput.innerHTML = response.temp+'℃';
cityOutput.innerHTML = cityname;}
).catch (err => console.error(err));
}
updateWeather('Delhi');