Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Based on internet connection change color of button to green or grey

I want the button itself to change its color when online(based on internet connection) to green otherwise gray.

I have a JS fiddle which gives a solution to click the button and we can see if we are online/offline by an alert.

Can someone please help

HTML:

<div data-role="page" id="index">
  <div data-theme="a" data-role="header">
    <h3>First Page</h3> <a href="#second" class="ui-btn-right">Next</a> </div>
  <div data-role="content"> <a data-role="button" id="check-connection">Check internet connection</a> </div>
</div>

jQuery:

$(document).on('pagebeforeshow', '#index', function() {
  $(document).on('click', '#check-connection', function() {
    var status = navigator.onLine ? 'online' : 'offline';
    (alert(status));
  });
});

See JSFiddle.

like image 641
Bob Avatar asked Nov 19 '16 00:11

Bob


People also ask

How do you change the color of a button?

All style elements in your HTML button tag should be placed within quotation marks. Type background-color: in the quotation marks after "style=". This element is used to change the background color of the button. Type a color name or hexadecimal code after "background-color:".

How do you change the color of the submit button in HTML?

You can easily change the color of the Next, Back and Submit buttons as well as the Progress bar on the Style tab of your survey. To do so simply go to Style > Button/Accent. The Button/Accent color applies to the Next, Back and Submit buttons as well as the progress bar.

How do I change the color of a clicked button in CSS?

To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the . button selector, you use background-color:#0a0a23; to change the background color of the button.

How do you change the button color when you click on a flutter?

Go to your main. Inside the MaterialApp, find the ThemeData widget. Add the elevatedButtonTheme property inside and assign the ElevatedButtonThemeData(). Add the style property and change the color as you would change it for normal ElevatedButton. Place the ElevatedButton widget anywhere in your Flutter app and see.


2 Answers

Here is the working fiddle: http://jsfiddle.net/dn7zjm41/

I just added an if statement:

if(status==="online") {
  $("#check-connection > span").css("background-color", "green");
} else if(status==="offline") {
  $("#check-connection > span").css("background-color", "gray");
} else {
  // the code for another scenario
}

And if you want an automatic run without click event - here it is: http://jsfiddle.net/f6paa9xy/

In this case your Javascript should look like this:

$(document).on('pagebeforeshow', '#index', function() {             
  var status = navigator.onLine ? 'online' : 'offline';
  alert(status);
  if(status==="online") {
    $("#check-connection > span").css("background-color", "green");
  } else if(status==="offline") {
    $("#check-connection > span").css("background-color", "red");
  }
});

Hope it helps

like image 108
Commercial Suicide Avatar answered Oct 20 '22 19:10

Commercial Suicide


You could use a simple css class to do this:

$(document).on('click', '#check-connection', function(){
    var status = navigator.onLine ? 'online' : 'offline';
    this.className = 'status-'+status;
});
.status-online{ background-color: green; }
.status-offline{ background-color: grey; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-role="content">
    <a data-role="button" id="check-connection">
      Check internet connection
  </a>
</div>

However, if you want a live determination, you can use an event listener provided by the browser, most support this (ie8+ for example):

window.addEventListener("offline", function() { 
    $("#check-connection")[0].className = "status-offline"; 
});
window.addEventListener("online", function() { 
    $("#check-connection")[0].className = "status-online"; 
});

More on this at MDN: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine

like image 38
Travis J Avatar answered Oct 20 '22 20:10

Travis J