-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
148 lines (129 loc) · 3.31 KB
/
index.html
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bootstrap demo</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="card">
<div class="search">
<input type="search" placeholder="enter city name" spellcheck="false" />
<button><img src="images/search.png" alt="" /></button>
</div>
<div class="weather">
<img src="images/rain.png" class="weather-icon" />
<h1 class="temp">22°C</h1>
<h2 class="city">New York</h2>
<div class="details">
<div class="col">
<img src="images/humidity.png" />
<div>
<p class="humidity">50%</p>
<p>humidity</p>
</div>
</div>
<div class="col">
<img src="images/wind.png" />
<div>
<p class="wind">15 km/hr</p>
<p>Wind Speed</p>
</div>
</div>
</div>
</div>
<!-- javascript code here -->
<!--
index.html:46
{coord: {…}, weather: Array(1), base: 'stations', main: {…}, visibility: 10000, …}
base
:
"stations"
clouds
:
all
:
100
[[Prototype]]
:
Object
cod
:
200
coord
:
{lon: -0.1257, lat: 51.5085}
dt
:
1693061437
id
:
2643743
main
:
{temp: 18.3, feels_like: 18.04, temp_min: 15.9, temp_max: 20.15, pressure: 1006, …}
name
:
"London"
rain
:
{1h: 0.11}
sys
:
{type: 2, id: 2075535, country: 'GB', sunrise: 1693026182, sunset: 1693076536}
timezone
:
3600
visibility
:
10000
weather
:
[{…}]
wind
:
{speed: 5.14, deg: 220}
[[Prototype]]
:
Object -->
<script>
const searchInput = document.querySelector('.search input');
const searchBtn = document.querySelector('.search button');
const image= document.querySelector('.weather-icon');
async function getWeather(city) {
try {
const apiKey = 'c925b7f1c5050b0035614084cfb137b8';
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`);
const data = await response.json();
console.log(data);
document.querySelector(".city").innerHTML = data.name;
document.querySelector(".wind").innerHTML = data.wind.speed + " km/hr";
document.querySelector(".temp").innerHTML = Math.floor(data.main.temp) + "°C";
document.querySelector(".humidity").innerHTML = data.main.humidity;
if(data.weather[0].main == "Clouds"){
image.src="./images/clouds.png"
}else if(data.weather[0].main == "Clear"){
image.src="./images/clear.png"
}else if(data.weather[0].main == "Rain"){
image.src="./images/rain.png"
}else if(data.weather[0].main == "Drizzle"){
image.src="./images/drizzle.png"
}else if(data.weather[0].main == "Mist"){
image.src="./images/mist.png"
}
} catch (error) {
console.error('Error fetching weather data:', error);
// You might want to display an error message on your UI here.
}
}
searchBtn.addEventListener('click', () => {
const city = searchInput.value;
if (city.trim() !== '') {
getWeather(city);
}
});
</script>
</body>
</html>