Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: form submitting PR #1

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 46 additions & 57 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,49 @@
document.addEventListener('DOMContentLoaded', () => {
const inputBox = document.querySelector('.input-box');
const searchBtn = document.getElementById('searchBtn');
const weather_img = document.querySelector('.weather-img');
const temperature = document.querySelector('.temperature');
const description = document.querySelector('.description');
const humidity = document.getElementById('humidity');
const wind_speed = document.getElementById('wind-speed');
const locationError = document.querySelector('.location-not-found');
const weatherBody = document.querySelector('.weather-body');

async function checkweather(city) {
const api_Key = "3e2b8eb1f8d10ccf16c9cf7b95a9bd7b";
const api_url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${api_Key}`;

try {
const response = await fetch(api_url);
const weatherData = await response.json();

if (weatherData.cod === '404') {
locationError.style.display = "flex";
weatherBody.style.display = "none";
return;
}

locationError.style.display = "none";
weatherBody.style.display = "flex";
temperature.innerHTML = `${Math.round(weatherData.main.temp - 273.15)}°C`;
description.innerHTML = `${weatherData.weather[0].description}`;
humidity.innerHTML = `${weatherData.main.humidity}%`;
wind_speed.innerHTML = `${weatherData.wind.speed} Km/H`;

switch (weatherData.weather[0].main) {
case 'Clouds':
weather_img.src = "./assets/cloud.png";
break;
case "Clear":
weather_img.src = "./assets/clear.png";
break;
case "Rain":
weather_img.src = "./assets/rain.png";
break;
case "Mist":
weather_img.src = "./assets/mist.png";
break;
case "Snow":
weather_img.src = "./assets/snow.png";
break;
default:
weather_img.src = "./assets/default.png";
}
} catch (error) {
console.error("Failed to fetch weather data:", error);
}
document.addEventListener("DOMContentLoaded", () => {
const form = document.querySelector(".search-box");
const inputBox = document.querySelector(".input-box");
const searchBtn = document.getElementById("searchBtn");
const weather_img = document.querySelector(".weather-img");
const temperature = document.querySelector(".temperature");
const description = document.querySelector(".description");
const humidity = document.getElementById("humidity");
const wind_speed = document.getElementById("wind-speed");
const locationError = document.querySelector(".location-not-found");
const weatherBody = document.querySelector(".weather-body");

async function checkweather(city) {
const api_Key = "3e2b8eb1f8d10ccf16c9cf7b95a9bd7b";
const api_url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${api_Key}`;

try {
const response = await fetch(api_url);
const weatherData = await response.json();

if (weatherData.cod === "404") {
locationError.style.display = "flex";
weatherBody.style.display = "none";
return;
}

locationError.style.display = "none";
weatherBody.style.display = "flex";
temperature.innerHTML = `${Math.round(weatherData.main.temp - 273.15)}°C`;
description.innerHTML = `${weatherData.weather[0].description}`;
humidity.innerHTML = `${weatherData.main.humidity}%`;
wind_speed.innerHTML = `${weatherData.wind.speed} Km/H`;

const icon = weatherData.weather[0].icon;

weather_img.src = `https://openweathermap.org/img/wn/${icon}.png`;

form.reset();
} catch (error) {
console.error("Failed to fetch weather data:", error);
}
}

form.addEventListener("submit", (event) => {
event.preventDefault();

searchBtn.addEventListener('click', () => {
checkweather(inputBox.value);
});
checkweather(inputBox.value);
});
});
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<body>
<div class="container">
<div class="header">
<div class="search-box">
<form class="search-box">
<input type="text" placeholder="Enter your location" class="input-box">
<button class="fa-solid fa-magnifying-glass" id="searchBtn"></button>
</div>
</form>
</div>
<div class="location-not-found">
<h1>Sorry, location not found!</h1>
Expand Down