-
Notifications
You must be signed in to change notification settings - Fork 4
Description
const apiKey = 'YOUR_API_KEY_HERE'; // Replace with your OpenWeatherMap API key
const searchBtn = document.getElementById('search-btn');
const cityInput = document.getElementById('city-input');
const weatherInfo = document.getElementById('weather-info');
searchBtn.addEventListener('click', () => {
const city = cityInput.value;
if (city) {
fetch(https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric)
.then(response => response.json())
.then(data => {
if (data.cod === 200) {
weatherInfo.innerHTML = <h2>${data.name}, ${data.sys.country}</h2> <p>Temperature: ${data.main.temp}°C</p> <p>Weather: ${data.weather[0].description}</p> <p>Humidity: ${data.main.humidity}%</p>;
} else {
weatherInfo.innerHTML = '
City not found. Try again.
';}
})
.catch(error => console.error('Error:', error));
}
});