I am trying to change the color of thermometer which I get from font awesome but now when i am trying to change it’s color so at that time it’s not working and when i assign new variable on top of my function to check is there any problem with my code which i wrote in my function although the new variable is assigned completely right but after assigning the new variable on top the rest of the code stop working, if i comment the new variable which i created on top the code start working perfectly
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
background-color: black;
color: white;
}
.container{
width: 100vw;
height: 100vh;
display: flex;`your text`
align-items: center;
justify-content: center;
font-size: 30px;
}
</style>
</head>
<body>
<div class="container">
<h1 class="heading">I am <br> Thermometer <span id="icon"></span></h1>
</div>
<script>
const heading = document.getElementsByClassName("heading")
heading.style.color="red";
const thermometer = ()=>{
let temp = document.getElementById("icon")
temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-empty"></i>'
setTimeout(()=>{
temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-quarter"></i>';
temp.style.Color ="#d63031"
},1000);
setTimeout(()=>{
temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-half"></i>';
},2000)
setTimeout(()=>{
temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-three-quarters"></i>'
},3000)
setTimeout(()=>{
temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-full"></i>'
},4000)
}
thermometer()
setInterval(thermometer,5000)
</script>
<script src="https://kit.fontawesome.com/8c672f3d7e.js" crossorigin="anonymous"></script>
</body>
</html>
4
Answers
Javascript is case-sensitive
This line
should read:
You may have been distracted because HTML/CSS is often case-insensitive
For example, you can call a tag
<DIV>
or<div>
or evendIv
. But Javascript variables and properties are case-sensitive.A few things, the syntax is
color
you’ve used an uppercase.const heading = document.getElementsByClassName("heading");
should bedocument.querySelector('heading');
, otherwise console errors as it’s expecting several elements.You’re also assigning the color to the
<span id='icon'></span>
and not the icon itself, see code I’m sure you can think of a better way to implement this.Here’s the solution to fix it…
The main problem is with your reset to set the color of everything to white.
Resets are a bad idea to begin with, but especially with color. The reason this is causing your code not to work is that you are changing the color of the
<span>
, but the<i>
element inside it (implementing the font awesome icon), rather than inheriting from the<span>
as it normally would, is forced to white by your blanket reset.User agent stylesheets (browser default style) are there for a reason and they actually make your life as a web developer much easier. Don’t try to override them using the asterisk (*) selector.
Instead, just apply color at the body level and through normal inheritance this will apply to those elements for which color inheritance makes sense.
A snippet to demonstrate:
Maybe try add
in to the temp.innerHTML.
like this:
in to the temp.innerHTML.
like this: