I'm new to JavaScript and i'm making a weather app it works fine but the issue is in the icons when i type another country to see the weather, the new country icon goes on top of the previous country icon and i want to hide the previous icon and show the new one i tried the if statement and switch case method but it didn't work
here is my html code:
<form>
<input class="input" type="text" placeholder="type your country">
</form>
<button class="button">SUBMIT</button>
<div class="location">
<h1 class="timezone">TIMEZONE</h1>
<div id="icons" class="">
<img id="cloud" class="hide" src="./gifs/cloud.png">
<img id="clouds" class="hide" src="./gifs/clouds.png">
<img id="cloudy" class="hide" src="./gifs/cloudy.png">
<img id="rain" class="hide" src="./gifs/rain.png">
<img id="snowflake" class="hide" src="./gifs/snowflake.png">
<img id="storm" class="hide" src="./gifs/storm.png">
<img id="sun" class="hide" src="./gifs/sun.png">
<img id="wind" class="hide" src="./gifs/wind.png">
</div>
</div>
and here is my javascript code:
const temp = data.main.temp - 273.15 ;
const celsius = temp.toPrecision(3);
const descripiton = data.weather[0].description;
const name = data.name;
tempDeg.textContent = celsius;
tempDes.textContent = descripiton;
tmZn.textContent = name;
switch(descripiton){
case "mist" :
document.getElementById("wind").classList.remove("hide");
break;
case "clear sky" :
document.getElementById("sun").classList.remove("hide");
break;
case "broken clouds" :
document.getElementById("clouds").classList.remove("hide");
break;
case "shower rain" :
document.getElementById("rain").classList.remove("hide");
break;
case "thunderstorm" :
document.getElementById("storm").classList.remove("hide");
break;
case "snow" :
document.getElementById("snow").classList.remove("hide");
break;
case "few clouds" :
document.getElementById("cloudy").classList.remove("hide");
break;
case "scattered clouds" :
document.getElementById("cloud").classList.remove("hide");
break;
}
You need to hide everything using something like a for
loop.
var icons = document.querySelector("#icons").children;
for (var i=0; i<icons.length; i++) {
icons[i].classList.add("hide");
}
Then remove the hide class from the one you wish to show as you have done in your code.