-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
118 lines (110 loc) · 3.94 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather Forecast App</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 960px;
margin: 0 auto;
padding: 2rem;
}
h1 {
text-align: center;
margin-bottom: 2rem;
}
form {
margin-bottom: 2rem;
}
input[type="text"] {
padding: 0.5rem;
width: 100%;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 0.5rem;
border: none;
border-radius: 4px;
cursor: pointer;
}
.weather-info {
display: flex;
justify-content: space-between;
align-items: center;
border: 1px solid #ccc;
border-radius: 4px;
padding: 1rem;
margin-bottom: 1rem;
}
.weather-info:last-child {
margin-bottom: 0;
}
.weather-icon {
width: 50px;
height: 50px;
}
.weather-description {
font-size: 1.25rem;
font-weight: bold;
}
.weather-temperature {
font-size: 1.25rem;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Weather Forecast App</h1>
<form id="weather-form">
<input type="text" id="city-input" placeholder="Enter city name...">
<input type="submit" value="Get Weather">
</form>
<div id="weather-info-container"></div>
<script>
const form = document.getElementById('weather-form');
const cityInput = document.getElementById('city-input');
const weatherInfoContainer = document.getElementById('weather-info-container');
form.addEventListener('submit', (e) => {
e.preventDefault();
const cityName = cityInput.value.trim();
if (cityName) {
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=e009286c9f951e55b8acf35cbc1bc4ab&units=metric`)
.then(response => response.json())
.then(data => {
displayWeatherInfo(data);
cityInput.value = '';
})
.catch(error => {
console.error('Error fetching weather data:', error);
alert('Error fetching weather data. Please try again.');
});
}
});
function displayWeatherInfo(weatherData) {
const { name, main, weather } = weatherData;
const weatherIconUrl = `http://openweathermap.org/img/wn/${weather[0].icon}@2x.png`;
const weatherInfo = document.createElement('div');
weatherInfo.classList.add('weather-info');
const weatherIcon = document.createElement('img');
weatherIcon.classList.add('weather-icon');
weatherIcon.src = weatherIconUrl;
weatherIcon.alt = weather[0].description;
const weatherDescription = document.createElement('div');
weatherDescription.classList.add('weather-description');
weatherDescription.textContent = `${name}, ${main.temp}°C, ${weather[0].description}`;
const weatherTemperature = document.createElement('div');
weatherTemperature.classList.add('weather-temperature');
weatherTemperature.textContent = `Feels like: ${main.feels_like}°C`;
weatherInfo.appendChild(weatherIcon);
weatherInfo.appendChild(weatherDescription);
weatherInfo.appendChild(weatherTemperature);
weatherInfoContainer.appendChild(weatherInfo);
}
</script>
</body>
</html>