In this lesson, you'll create your own Weather Web App using HTML, CSS, JavaScript, and jQuery. The app will display real-time weather data for a specified location.
You'll learn how to add jQuery and Fontawesome, create an HTML structure, style it, initialise JavaScript, fetch and display weather data from an API, and add a feature to toggle between Celsius and Fahrenheit. By the end, you'll have a fully functional, interactive Weather Web App.
First, create a new workspace for this lesson in VS Code.
Open VS Code and follow these steps:
File
' > 'Open Folder
'.WeatherApp
' in your Documents folder.Now, create the basic HTML structure for your weather app, including sections for location input, weather display, and toggle button. We'll include jQuery via CDN for DOM manipulation and Fontawesome for icons. Fontawesome is a popular library that provides a wide collection of scalable vector icons that you can easily add to your web pages using simple HTML classes, making it straightforward to enhance your app with visual elements like weather symbols.
Create a new file called index.html
and add the following code to it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather Web App</title>
<link rel="stylesheet" href="css/styles.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Weather Web App</h1>
<div id="weather-app">
<input type="text" id="location" placeholder="Enter city name">
<button id="get-weather">Get Weather</button>
<div id="weather-info"></div>
<button id="toggle-unit">Toggle to Fahrenheit</button>
</div>
<script src="js/script.js"></script>
</body>
</html>
To make your weather app look appealing, add some CSS styles for the structure and headings.
Create a new css
folder and add a styles.css
file to it. Then add the following code to styles.css
:
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f8ff;
}
h1 {
color: #333;
}
#weather-app {
margin: 20px auto;
width: 60%;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
input[type="text"] {
padding: 10px;
width: 200px;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
cursor: pointer;
margin: 10px;
}
button:hover {
background-color: #0056b3;
}
#weather-info {
font-size: 1.2em;
margin-top: 20px;
}
Create a new js
folder and add a script.js
file to it. We'll initialise the JavaScript to handle button clicks and prepare for API calls.
Add the following complete JavaScript code to script.js
:
$(document).ready(function() {
let unit = 'metric'; // Default to Celsius
$('#get-weather').click(function() {
const location = $('#location').val();
if (location) {
// Placeholder for API call
$('#weather-info').html('<p>Fetching weather for ' + location + '...</p>');
} else {
alert('Please enter a location');
}
});
$('#toggle-unit').click(function() {
unit = (unit === 'metric') ? 'imperial' : 'metric';
$(this).text((unit === 'metric') ? 'Toggle to Fahrenheit' : 'Toggle to Celsius');
// Placeholder for refreshing weather
});
});