I've a script on my page who should work if is internet connection ( i mean.. load text when click button or show less if is needed to ) and hide buttons if no connection.
I mention that I use jquery-3.3.1.slim.min.js (script src link-website) library.
I'm using tags directly to HTML head.
When is connection ( == 'true' ) there is a part with load more paragraphs / showless p . When is not internet I should hide those divs and show all text.
/*JQUERY CODE*/
var connection = "";
connection += navigator.onLine;
if (connection == 'true') {
$(document).ready(function() {
size_p = $("#myList p").length;
x = 0;
$('#myList p:lt(' + x + ')').show();
$('#loadMore').click(function() {
x = (x + 4 <= size_p) ? x + 4 : size_p;
$('#myList p:lt(' + x + ')').show();
});
$('#showLess').click(function() {
x = (x - 4 < 0) ? 4 : x - 4;
$('#myList p').not(':lt(' + x + ')').hide();
});
});
} else
if (connection == 'false') {
$("#loadMore").css("display", "none");
$("#showLess").css("display", "none");
$("#myList").css("display", "inline");
}
/*CSS CODE*/
#myList p {
display: none;
}
#loadMore {
color: green;
cursor: pointer;
padding: 5px 5px 5px 10px;
cursor: pointer;
background: #e8e8e8;
font-size: 14px;
width: 115px;
margin: 5px;
position: relative;
border-radius: 15px;
}
#loadMore:hover #showLessa:hover #showLess:hover {
color: black;
}
#showLess {
color: red;
cursor: pointer;
padding: 5px 5px 5px 10px;
cursor: pointer;
background: #e8e8e8;
font-size: 14px;
width: 115px;
margin: 5px;
position: relative;
border-radius: 15px;
}
#load_no_internet {
display: none;
color: green;
cursor: pointer;
padding: 5px 5px 5px 10px;
cursor: pointer;
background: #e8e8e8;
font-size: 14px;
width: 115px;
margin: 5px;
position: relative;
border-radius: 15px;
}
#load_no_internet:hover #show_no_interneta:hover #show_no_internet:hover {
color: black;
}
#show_no_internet {
display: none;
color: red;
cursor: pointer;
padding: 5px 5px 5px 10px;
cursor: pointer;
background: #e8e8e8;
font-size: 14px;
width: 115px;
margin: 5px;
position: relative;
border-radius: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p> SOME TEXT HERE </p>
<p> SOME TEXT HERE </p>
<p> SOME TEXT HERE </p>
<p> SOME TEXT HERE </p>
<p> SOME TEXT HERE </p>
<p> SOME TEXT HERE </p>
<p> SOME TEXT HERE </p>
<div id="myList">
<p> SOME TEXT HERE </p>
<p> SOME TEXT HERE </p>
<p> SOME TEXT HERE </p>
<p> SOME TEXT HERE </p>
</div>
<div id="loadMore">See more text...</div>
<div id="showLess">See less text...</div>
My problem is that when I have no internet.. nothing work.
I would like to give me some advice how to make it work if no internet connection.