OneCompiler

HTML SPINNING FORM

116
<!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="styles.css"> </head> <body> <div class="container"> <h2>Weather App</h2> <input type="text" id="city" placeholder="Enter city name"> <button onclick="getWeather()">Get Weather</button> <div id="weather-info"></div> </div> <script src="script.js"></script> </body> </html> <style> body { font-family: Arial, sans-serif; text-align: center; }

.container {
width: 300px;
margin: 50px auto;
padding: 20px;
background:white;
box-shadow: -1px -1px 15px rgba(0,0,0,1) inset;
border-radius:15px;
}
input {
width: 80%;
padding: 8px;
margin-bottom:30px;
outline:0px;
}
button {
padding: 8px 45px;
background: #28a745;
color:white;
border: none;
cursor: pointer;
border-radius:6px;
}
button:hover {
background: #218838;
}
#weather-info {
margin-top: 20px;
font-size: 18px;
}
</style>

<script> async function getWeather() { const city = document.getElementById('city').value; if (!city) { alert("Please enter a city name"); return; } const apiKey = "a55e2d70ab5a447e813154451250903"; const url = `http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${city}&aqi=yes`; try { const response = await fetch(url); const data = await response.json(); if (data.error) { document.getElementById('weather-info').innerHTML = "City not found!"; return; } document.getElementById('weather-info').innerHTML = ` <p><strong>Location:</strong> ${data.location.name}, ${data.location.country}</p> <p><strong>Temperature:</strong> ${data.current.temp_c}°C</p> <p><strong>Condition:</strong> ${data.current.condition.text}</p> <p><strong>Air Quality Index:</strong> ${data.current.air_quality.pm2_5}</p> <img src="${data.current.condition.icon}" alt="Weather Icon"> `; } catch (error){ document.getElementById('weather-info').innerHTML = "Error fetching weather data!"; } } </script>