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.
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:".
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With