-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.html
93 lines (78 loc) · 2.29 KB
/
weather.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather App</title>
<link rel="stylesheet" href="styles.css">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
text-align: center;
}
.weather-container {
margin: 50px auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
border-radius: 10px;
width: 300px;
}
h1 {
font-size: 24px;
margin-bottom: 10px;
}
input {
width: 90%;
padding: 10px;
margin-bottom: 10px;
}
button {
background-color: #007BFF;
color: #fff;
border: none;
padding: 10px 20px;
cursor: pointer;
}
#weather-info {
margin-top: 20px;
}
</style>
</head>
<body>
<div class="weather-container">
<h1>Weather App</h1>
<input type="text" id="city" placeholder="Enter a city">
<button id="get-weather">Get Weather</button>
<div id="weather-info"></div>
</div>
<script >
document.addEventListener('DOMContentLoaded', () => {
const apiKey = '6103a72123685dab9eab6bc9f0ab10c1';
const getWeatherButton = document.getElementById('get-weather');
const weatherInfo = document.getElementById('weather-info');
getWeatherButton.addEventListener('click', () => {
const cityInput = document.getElementById('city');
const city = cityInput.value;
if (!city) {
alert('Please enter a city');
return;
}
fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`)
.then((response) => response.json())
.then((data) => {
const temperature = (data.main.temp - 273.15).toFixed(2); // t---->cel
const description = data.weather[0].description;
const weatherText = `Temperature: ${temperature}°C<br>Condition: ${description}`;
weatherInfo.innerHTML = weatherText;
})
.catch((error) => {
console.error('Error fetching weather data:', error);
weatherInfo.innerHTML = 'Error fetching weather data';
});
});
});
</script>
</body>
</html>