-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
45 lines (44 loc) · 1.35 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
const searchBtn = document.querySelector('button');
let cityInput = '';
let temp = '';
let cityName = '';
let condition = '';
let weatherImgSrc = '';
searchBtn.addEventListener('click', () => {
getUserValue();
showWeather();
});
function getUserValue() {
const searchBar = document.querySelector('input').value;
// check for just letter and nothing else
if (!searchBar) {
console.log('no value');
} else {
cityInput = searchBar;
console.log(cityInput);
}
}
async function showWeather() {
const response = await fetch(
`https://api.weatherapi.com/v1/current.json?key=abe35ea518404fca9b1122150232907&q=${cityInput}&aqi=no
`,
{ mode: 'cors' }
);
const current = response.json().then(function (response) {
temp = response.current.feelslike_c;
condition = response.current.condition.text;
weatherImgSrc = response.current.condition.icon;
cityName = response.location.name + ' ' + response.location.country;
DOM();
});
function DOM() {
const weatherImg = document.querySelector('img');
const location = document.querySelector('.city-name');
const cityTemp = document.querySelector('.temp');
const cityCondition = document.querySelector('.condition');
weatherImg.src = weatherImgSrc;
location.innerHTML = `<span class="fas fa-map-marker-alt"> </span> ${cityName}`;
cityTemp.innerText = temp;
cityCondition.innerText = condition;
}
}