Build dynamic web applications with API integrations and interactive UI components.
Start the Course ➡️Learn to fetch and display data from external APIs in your web applications.
Create responsive and interactive user interfaces with JavaScript.
Implement error handling and data validation in your web apps.
Organize your codebase with proper file structure and dependencies.
In this tutorial we'll create a weather application that fetches current weather data using the OpenWeatherMap API.
We'll be using plain HTML/CSS/JavaScript with no frameworks for this intermediate example. Let's set up our project structure:
weather-app/
├── index.html
├── style.css
├── script.js
└── config.js
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
<title>Weather App</title>
</head>
<body>
<div class="container">
<h1>Current Weather</h1>
<form id="weather-form">
<input type="text" id="city-input" placeholder="Enter city..." required>
<button type="submit">Get Weather</button>
</form>
<div id="weather-result"></div>
</div>
<script src="script.js"></script>
<body>
</html>
Let's add modern styling to our weather application interface.
Add these styles to your style.css file for the weather app:
body {
font-family: 'Segoe UI', sans-serif;
background: linear-gradient(to right, #fffde7, #fff9c4);
display: flex;
justify-content: center;
min-height: 100vh;
}
.container {
max-width: 500px;
margin: 2rem auto;
padding: 2rem;
background: white;
border-radius: 1rem;
box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1);
}
form {
display: flex;
gap: 0.5rem;
margin-bottom: 2rem;
}
input[type="text"] {
flex-grow: 1;
padding: 1rem;
border: 2px solid #fff9c4;
border-radius: 0.5rem;
font-size: 1rem;
}
button {
padding: 0.75rem 1.5rem;
background: #fbc02d;
color: white;
border: none;
border-radius: 0.5rem;
cursor: pointer;
transition: all 0.3s ease;
}
button:hover {
transform: scale(1.05);
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
#weather-result {
text-align: center;
border-top: 2px solid #ffeeba;
padding-top: 2rem;
}
Now let's add JavaScript to fetch and display weather data dynamically.
Add this JavaScript to your script.js file to make the weather app work:
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('weather-form');
const input = document.getElementById('city-input');
const weatherResult = document.getElementById('weather-result');
form.addEventListener('submit', async function(e) {
e.preventDefault();
const city = input.value.trim();
if (!city) return;
weatherResult.innerHTML = `
`;
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${encodeURIComponent(city)}&appid=YOUR_API_KEY&units=metric`)
.then(response => {
if (!response.ok) {
throw new Error('City not found');
}
return response.json();
})
.then(data => {
const weather = data;
const description = weather.weather[0].description;
const temp = weather.main.temp;
const humidity = weather.main.humidity;
const wind = weather.wind.speed;
weatherResult.innerHTML = `
${weather.name}
${data.weather[0].main}
Temperature: ${temp}°C
Humidity: ${humidity}%
Wind: ${wind} m/s
`;
input.value = '';
})
.catch(error => {
weatherResult.innerHTML = `
${error.message} - Try checking the city spelling
`;
});
});
});
Deploy your weather app to GitHub Pages or Netlify to make it accessible to the world.
Next: Advanced Web App Features