Build a Real-Time Weather App using JavaScript π¦️
π¦️ Build a Real-Time Weather App using JavaScript
Hello everyone π
Welcome back!
In this project, we will build a Real-Time Weather App using HTML, CSS & JavaScript π¦️
This project is perfect for beginners who want to learn:
- API Integration
- Fetch API
- Async/Await
- DOM Manipulation
- Real-world JavaScript Projects
---
π Features of Weather App
✔ Search weather by city
✔ Real-time weather data
✔ Temperature in °C
✔ Weather conditions
✔ Responsive design
✔ Beginner-friendly project
---
π§± Step 1: HTML Code
HTML
<!DOCTYPE html>
<html>
<head>
<title>Weather App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<h1>Weather App</h1>
<div class="search-box">
<input type="text" id="city" placeholder="Enter city name">
<button onclick="getWeather()">Search</button>
</div>
<div id="result"></div>
</div>
<script src="script.js"></script>
</body>
</html>
---
π¨ Step 2: CSS Code
CSS
*{
margin:0;
padding:0;
box-sizing:border-box;
font-family:Arial;
}
body{
height:100vh;
display:flex;
justify-content:center;
align-items:center;
background:linear-gradient(135deg,#4facfe,#00f2fe);
}
.container{
background:white;
padding:30px;
width:400px;
border-radius:15px;
text-align:center;
box-shadow:0 10px 25px rgba(0,0,0,0.2);
}
h1{
margin-bottom:20px;
}
.search-box{
display:flex;
gap:10px;
}
input{
flex:1;
padding:10px;
border:2px solid #ddd;
border-radius:8px;
}
button{
padding:10px 15px;
background:#007bff;
color:white;
border:none;
border-radius:8px;
cursor:pointer;
}
#result{
margin-top:20px;
}
---
⚡ Step 3: JavaScript Code
JavaScript
async function getWeather(){
const city = document.getElementById("city").value;
const apiKey = "YOUR_API_KEY";
const url =
`https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;
try{
const response = await fetch(url);
const data = await response.json();
if(data.cod === "404"){
document.getElementById("result").innerHTML =
"<p style='color:red;'>City not found!</p>";
return;
}
document.getElementById("result").innerHTML = `
<h2>${data.name}</h2>
<h3>${Math.round(data.main.temp)}°C</h3>
<p>${data.weather[0].description}</p>
<p>Humidity: ${data.main.humidity}%</p>
<p>Wind Speed: ${data.wind.speed} km/h</p>
`;
}catch(error){
document.getElementById("result").innerHTML =
"<p>Something went wrong!</p>";
}
}
---
π Get Free API Key
π Create a free account on
"openweathermap.org" (https://reference-url-citation.invalid/0)
Then generate your free API key.
Replace:
JavaScript
YOUR_API_KEY
with your actual API key.
---
π― What You Will Learn
✅ API Integration
✅ Fetch Data using JavaScript
✅ Async/Await
✅ DOM Manipulation
✅ Real-world Project Development
---
⭐ Tips to Improve Project
π Add weather icons ☀️
π Add dark mode π
π Show more details like humidity & wind speed
π Add loading animation ⚡
---
π Conclusion
This was a Real-Time Weather App Project using JavaScript π¦️
A perfect beginner project to improve your JavaScript skills and build real-world applications π
---
π’ Also Read
π Calculator Project
π To-Do App Project
π JavaScript Interview Guide
---
π Save and share this project π
π Follow for more coding content π»
Comments
Post a Comment