In this lesson, you'll learn how to integrate external libraries and APIs into a web project. Libraries like jQuery simplify JavaScript tasks, while APIs provide data from external sources.
You'll set up a new workspace in VS Code, add jQuery, create an HTML structure for a weather app, learn jQuery syntax, selectors, and events, add a click event listener to fetch data from the OpenWeatherMap API, display the weather for a location, and tackle a challenge to show more data.
First, create a new workspace for this lesson in VS Code.
Open VS Code and follow these steps:
File
' > 'Open Folder
'.WeatherAPI
' in your Documents folder.Now, create the basic HTML structure with a button and a display area.
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 App</title>
<link rel="stylesheet" href="css/styles.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Dublin Weather</h1>
<button id="getWeather">Get Weather</button>
<div id="weatherDisplay"></div>
<script src="js/script.js"></script>
</body>
</html>
To style your page, create a CSS file.
Create a new css
folder and add a styles.css
file to it. Next add the following code to the new styles.css
file:
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
}
h1 {
color: navy;
}
button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#weatherDisplay {
margin-top: 20px;
font-size: 1.2em;
}
Next, create a new js
folder and add a script.js
file to it.
For now, leave 'script.js
' empty.