-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweatherapp.html
More file actions
61 lines (55 loc) · 2.04 KB
/
weatherapp.html
File metadata and controls
61 lines (55 loc) · 2.04 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
<!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>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
#weather-container {
text-align: center;
}
</style>
</head>
<body>
<div id="weather-container">
<h1>Weather App</h1>
<label for="city">Enter City:</label>
<input type="text" id="city" placeholder="City">
<button onclick="getWeather()">Get Weather</button>
<div id="weather-info"></div>
</div>
<script>
function getWeather() {
const apiKey = 'e502a068955c85488c55c8f15ab35747';
const city = document.getElementById('city').value;
if (!city) {
alert('Please enter a city name');
return;
}
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`)
.then(response => response.json())
.then(data => {
const weatherInfo = document.getElementById('weather-info');
if (data.cod === '404') {
weatherInfo.innerHTML = `<p>${data.message}</p>`;
} else {
const temperature = data.main.temp;
const description = data.weather[0].description;
const cityName = data.name;
weatherInfo.innerHTML = `<p>Temperature in ${cityName}: ${temperature}°C</p>
<p>Weather: ${description}</p>`;
}
})
.catch(error => console.error('Error fetching weather data:', error));
}
</script>
</body>
</html>