Search code examples
javascripthtmlcsscolorsruntime-error

Color is not changing by Javascript


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>

Solution

  • 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>