skip to Main Content

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


  1. Javascript is case-sensitive

    This line

              temp.style.Color ="#d63031"
    

    should read:

              temp.style.color ="#d63031"
    

    You may have been distracted because HTML/CSS is often case-insensitive

    For example, you can call a tag <DIV> or <div> or even dIv. But Javascript variables and properties are case-sensitive.

    Login or Signup to reply.
  2. A few things, the syntax is color you’ve used an uppercase.

    const heading = document.getElementsByClassName("heading"); should be document.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…

     <script>
            const heading = document.querySelector('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" style="color: #d63031"></i>';
                }, 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>
    
    Login or Signup to reply.
  3. The main problem is with your reset to set the color of everything to white.

    * {color: 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.

    body {color: white;}
    

    A snippet to demonstrate:

    const heading = document.querySelector(".heading")
    heading.style.color="red"
    
    const thermometer = () => {
      let temp = document.getElementById("icon")
      temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-empty"></i>'
      temp.style.color ="white"
    
      setTimeout(()=>{
        temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-quarter"></i>'
        temp.style.color ="lime"
      },1000)
      
      setTimeout(()=>{
        temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-half"></i>'
        temp.style.color ="yellow"
      },2000)
    
      setTimeout(()=>{
        temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-three-quarters"></i>'
        temp.style.color ="orange"
      },3000)
    
      setTimeout(()=>{
        temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-full"></i>'
        temp.style.color ="red"
      },4000)
    
    }
    
    thermometer()
    setInterval(thermometer,5000)
    body {
      background-color: black;
      color: white;
    }
    
    .container {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 30px;
    }
    <script src="https://kit.fontawesome.com/8c672f3d7e.js"></script>
          
    <div class="container">
      <h1 class="heading">I am <br> Thermometer <span id="icon"></span></h1>
    </div>
    Login or Signup to reply.
  4. Maybe try add

    style="color: red"
    

    in to the temp.innerHTML.

    like this:

    style="color: red"
    

    in to the temp.innerHTML.

    like this:

    temp.innerHTML='<i class="fa-sharp fa-solid fa-temperature-empty" style="color:red"></i>'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search