-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (51 loc) · 1.84 KB
/
script.js
File metadata and controls
65 lines (51 loc) · 1.84 KB
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
const apiKey="73cf12a1bbd4b8ee8a5c09799393fdf2";
const apiURL="https://api.openweathermap.org/data/2.5/weather?units=metric&q=";
const searchBox=$("input");
const searchButton=$("button");
const imageSelect=$(".weatherReport");
async function checkWeather(city){
const response=await fetch(apiURL+ city +`&appid=${apiKey}`);
if(response.status===404){
$(".error").css("display", "block");
$(".weather-icon").css("display", "none");
}
else{
var data=await response.json();
$(".city").text(data.name);
$(".temp").text(Math.round(data.main.temp)+"°C");
$(".humidity").text(data.main.humidity+"%");
$(".wind").text(data.wind.speed+"kmph");
if (data.weather[0].main === "Clouds") {
imageSelect.attr("src", "assets/images/clouds.png");
} else if (data.weather[0].main === "Clear") {
imageSelect.attr("src", "assets/images/clear.png");
} else if (data.weather[0].main === "Drizzle") {
imageSelect.attr("src", "assets/images/drizzle.png");
} else if (data.weather[0].main === "Mist") {
imageSelect.attr("src", "assets/images/mist.png");
} else if (data.weather[0].main === "Rain") {
imageSelect.attr("src", "assets/images/rain.png");
} else if (data.weather[0].main === "Snow") {
imageSelect.attr("src", "assets/images/snow.png");
} else {
console.log("Error");
}
$(".weather-icon").css("display", "block");
$(".error").css("display", "none");
}
}
function handleSearch() {
const city = searchBox.val();
checkWeather(city);
}
searchButton.click(function(){
handleSearch();
});
searchBox.keydown(function(event){
if(event.which=== 13) {
handleSearch();
}
else{
console.log("Error");
}
});