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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...