Step 1: Set Up Your Project

Step 1: Set Up Your Project

Create a new folder for your project and add the following files:

  • index.html
  • styles.css
  • script.js

Step 2: HTML Structure

In index.html, add the following code:

htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;Weather App&lt;/title&gt;
&lt;link rel="stylesheet" href="styles.css"&gt;
</head> <body>
&lt;div class="container"&gt;
    &lt;h1&gt;Weather App&lt;/h1&gt;
    &lt;input type="text" id="cityInput" placeholder="Enter city name"&gt;
    &lt;button id="getWeatherBtn"&gt;Get Weather&lt;/button&gt;
    &lt;div id="weatherResult"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;script src="script.js"&gt;&lt;/script&gt;
</body> </html>

Step 3: CSS Styling

In styles.css, add some basic styles:

cssCopy codebody {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
} .container {
text-align: center;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
} input {
padding: 10px;
margin: 10px 0;
width: 200px;
} button {
padding: 10px 20px;
} #weatherResult {
margin-top: 20px;
}

Step 4: JavaScript Logic

In script.js, write the logic to fetch weather data from the API:

javascriptCopy codeconst apiKey = 'YOUR_API_KEY'; // Replace with your OpenWeatherMap API key

document.getElementById('getWeatherBtn').addEventListener('click', getWeather);

async function getWeather() {
const city = document.getElementById('cityInput').value;
if (!city) {
    alert('Please enter a city name');
    return;
}
const url = https://api.openweathermap.org/data/2.5/weather?q=${city}&amp;amp;appid=${apiKey}&amp;amp;units=metric;
try {
    const response = await fetch(url);
    if (!response.ok) throw new Error('City not found');
    
    const data = await response.json();
    displayWeather(data);
} catch (error) {
    document.getElementById('weatherResult').innerText = error.message;
}
} function displayWeather(data) {
const { name, main, weather } = data;
const weatherResult = `
    &lt;h2&gt;Weather in ${name}&lt;/h2&gt;
    &lt;p&gt;Temperature: ${main.temp}°C&lt;/p&gt;
    &lt;p&gt;Condition: ${weather&#91;0].description}&lt;/p&gt;
`;
document.getElementById('weatherResult').innerHTML = weatherResult;
}

Step 5: Get Your API Key

  1. Sign up at OpenWeatherMap.
  2. Get your API key from the API keys section.
  3. Replace YOUR_API_KEY in the JavaScript code with your actual API key.

Step 6: Run Your App

Open index.html in your web browser. You should see a simple weather app where you can input a city name and get the current weather.Create a new folder for your project and add the following files:

  • index.html
  • styles.css
  • script.js

Step 2: HTML Structure

In index.html, add the following code:

htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;Weather App&lt;/title&gt;
&lt;link rel="stylesheet" href="styles.css"&gt;
</head> <body>
&lt;div class="container"&gt;
    &lt;h1&gt;Weather App&lt;/h1&gt;
    &lt;input type="text" id="cityInput" placeholder="Enter city name"&gt;
    &lt;button id="getWeatherBtn"&gt;Get Weather&lt;/button&gt;
    &lt;div id="weatherResult"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;script src="script.js"&gt;&lt;/script&gt;
</body> </html>

Step 3: CSS Styling

In styles.css, add some basic styles:

cssCopy codebody {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
} .container {
text-align: center;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
} input {
padding: 10px;
margin: 10px 0;
width: 200px;
} button {
padding: 10px 20px;
} #weatherResult {
margin-top: 20px;
}

Step 4: JavaScript Logic

In script.js, write the logic to fetch weather data from the API:

javascriptCopy codeconst apiKey = 'YOUR_API_KEY'; // Replace with your OpenWeatherMap API key

document.getElementById('getWeatherBtn').addEventListener('click', getWeather);

async function getWeather() {
const city = document.getElementById('cityInput').value;
if (!city) {
    alert('Please enter a city name');
    return;
}
const url = https://api.openweathermap.org/data/2.5/weather?q=${city}&amp;amp;appid=${apiKey}&amp;amp;units=metric;
try {
    const response = await fetch(url);
    if (!response.ok) throw new Error('City not found');
    
    const data = await response.json();
    displayWeather(data);
} catch (error) {
    document.getElementById('weatherResult').innerText = error.message;
}
} function displayWeather(data) {
const { name, main, weather } = data;
const weatherResult = `
    &lt;h2&gt;Weather in ${name}&lt;/h2&gt;
    &lt;p&gt;Temperature: ${main.temp}°C&lt;/p&gt;
    &lt;p&gt;Condition: ${weather&#91;0].description}&lt;/p&gt;
`;
document.getElementById('weatherResult').innerHTML = weatherResult;
}

Step 5: Get Your API Key

  1. Sign up at OpenWeatherMap.
  2. Get your API key from the API keys section.
  3. Replace YOUR_API_KEY in the JavaScript code with your actual API key.

Step 6: Run Your App

Open index.html in your web browser. You should see a simple weather app where you can input a city name and get the current weather.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *