Just Jolly Functions
Functions: Adding a Weather Widget with HTML, CSS, and JavaScript
A simple Weather Widget using HTML, CSS, and JavaScript. This widget fetches the weather data from the OpenWeatherMap API and displays the current weather in a city. You can replace "YOUR_API_KEY" with your actual API key from OpenWeatherMap.
Tip: Learn how to Insert HTML Code into Your Post.
Full Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather Widget</title>
<style>
/* Basic styling */
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f9;
margin: 0;
}
.weather-widget {
background-color: #fff;
border-radius: 10px;
padding: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}
.weather-widget h2 {
margin: 0;
font-size: 2rem;
color: #333;
}
.weather-widget p {
margin: 10px 0;
font-size: 1.1rem;
color: #555;
}
.weather-widget .temp {
font-size: 3rem;
color: #f39c12;
}
.weather-widget .description {
font-size: 1.2rem;
text-transform: capitalize;
}
.weather-widget .error {
color: red;
font-size: 1.2rem;
}
.weather-widget input {
padding: 10px;
margin-top: 15px;
width: 80%;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 1rem;
}
.weather-widget button {
padding: 10px;
background-color: #3498db;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 10px;
}
.weather-widget button:hover {
background-color: #2980b9;
}
</style>
</head>
<body>
<div class="weather-widget">
<h2>Weather</h2>
<p>Enter city to get the weather</p>
<input type="text" id="city-input" placeholder="Enter city name">
<button id="get-weather">Get Weather</button>
<div id="weather-info">
<p class="temp"></p>
<p class="description"></p>
<p class="error"></p>
</div>
</div>
<script>
// Get DOM elements
const cityInput = document.getElementById("city-input");
const getWeatherButton = document.getElementById("get-weather");
const weatherInfo = document.getElementById("weather-info");
const tempElement = weatherInfo.querySelector(".temp");
const descriptionElement = weatherInfo.querySelector(".description");
const errorElement = weatherInfo.querySelector(".error");
// Function to fetch weather
async function fetchWeather(city) {
const apiKey = "YOUR_API_KEY"; // Replace with your actual API key
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
try {
const response = await fetch(url);
const data = await response.json();
if (data.cod === "404") {
throw new Error("City not found");
}
const temp = data.main.temp;
const description = data.weather[0].description;
// Display weather info
tempElement.textContent = `${temp}°C`;
descriptionElement.textContent = description;
errorElement.textContent = ""; // Clear any error message
} catch (error) {
tempElement.textContent = "";
descriptionElement.textContent = "";
errorElement.textContent = "Error: " + error.message;
}
}
// Event listener for button click
getWeatherButton.addEventListener("click", () => {
const city = cityInput.value.trim();
if (city) {
fetchWeather(city);
} else {
errorElement.textContent = "Please enter a city name.";
}
});
</script>
</body>
</html>
Explanation:
HTML Structure:
The Weather Widget contains an input field where the user can type a city name.
A button is provided to trigger the weather search.
The weather information (temperature, description) or an error message is displayed below.
CSS Styling:
The widget is styled with a clean design and centered on the page.
The temperature (.temp) is styled in a large font with a warm color (#f39c12).
The widget background is white with rounded corners and a subtle shadow for a modern look.
There are hover effects on the button to provide interactive feedback to users.
JavaScript:
The script fetches weather data from the OpenWeatherMap API using the fetch API.
It dynamically updates the widget with the temperature and description of the weather when the button is clicked.
Error handling is implemented to display an error message if the city is not found or if there is an issue with the API request.
How to Use:
Get an API Key:
Go to OpenWeatherMap and sign up for a free account.
After registering, you will get an API key. Replace "YOUR_API_KEY" in the code with your actual API key.
Customization:
You can modify the appearance (e.g., colors, fonts, etc.) by updating the CSS.
You can also add more weather data, like humidity or wind speed, by extracting additional information from the API response.
Features:
Responsive: The widget is simple and can adjust to various screen sizes.
Error Handling: It handles errors such as incorrect city names and missing input.
Simple and Interactive: A clean UI with clear user interaction, making it easy to use.
This is a simple and effective way to display weather information dynamically on your post.