Jump to content
Sign in to follow this  
s_avinash_s

getting a red and green dot

Recommended Posts

Hi

I have a below code which works perfectly when Ethernet cable is connected or disconnected.

 

        <p id="ping1">      
        </p>
    <script>    
    function ping() 
    {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() 
        {
            if(xhttp.readyState == 4) 
            {
                if(xhttp.status == 0) 
                {      
                    document.getElementById("ping1").innerHTML = "Disconnected";
                    document.getElementById("ping1").style.color = "red";                  
                }
                else 
                {       
                    document.getElementById("ping1").innerHTML = "Connected";                  
                    document.getElementById("ping1").style.color = "green";                                       
                }
            }
        };
    xhttp.open("GET", "ping.txt", true);
    xhttp.send();
    }
     setInterval(ping(), 1000);
        
    </script>

Now i want to modify the below code 

                if(xhttp.status == 0) 
                {      
                    document.getElementById("ping1").innerHTML = "Disconnected";
                    document.getElementById("ping1").style.color = "red";                  
                }
                else 
                {       
                    document.getElementById("ping1").innerHTML = "Connected";                  
                    document.getElementById("ping1").style.color = "green";                                       
                }

with a red button for disconnected and with green for connected.(button like small dot ).

I got the below code for dots creation just need to change a color.But how to use in my code for  xhttp.status == 0 and else part

 

Please suggest a way to do it

Edited by s_avinash_s

Share this post


Link to post
Share on other sites

Hi 

I have got a sample code for LED  change.

https://www.w3schools.com/code/tryit.asp?filename=FW1SQNHXYORW

How to get led change in below conditions using div as shown above.

 if(xhttp.status == 0) 
                {      
                    document.getElementById("ping1").innerHTML = "Disconnected";
                    document.getElementById("ping1").style.color = "red";                  
                }
                else 
                {       
                    document.getElementById("ping1").innerHTML = "Connected";                  
                    document.getElementById("ping1").style.color = "green";                                       
                }

In above if status is 0 , i need to glow yellow LED else Green .How to do it.

Please help

 

Edited by s_avinash_s

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×