Ad

If All Divs Loaded

- 1 answer

I'm coding my blog theme. I have this button to load in the entires, but I want to make that button change to "No more entries" once all is loaded. Any clue?

  <script>
$(document).ready(function () {
    size_li = $(".all .one").size();
    x=3;
    $('.all .one:lt('+x+')').show();
    $('#loadMore').click(function () {
        x= (x+5 <= size_li) ? x+5 : size_li;
        $('.all .one:lt('+x+')').fadeIn();
    });

});
    </script>
Ad

Answer

Check if there are any hidden elements after starting to fade in the items. If there are no any, just show the message.

$(document).ready(function() {
  size_li = $(".all .one").size();
  x = 3;
  $('.all .one:lt(' + x + ')').show();
  $('#loadMore').click(function() {
    x = (x + 5 <= size_li) ? x + 5 : size_li;
    $('.all .one:lt(' + x + ')').fadeIn();
    if (!$(".all .one:hidden").length) {
      // Do something when everything is loaded
      $(this).replaceWith("<p>No more entries<p>");
    }
  });

});
.one {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="all">
  <div class="one">1</div>
  <div class="one">2</div>
  <div class="one">3</div>
  <div class="one">4</div>
  <div class="one">5</div>
  <div class="one">6</div>
  <div class="one">7</div>
  <div class="one">8</div>
  <div class="one">9</div>
  <div class="one">10</div>
</div>
<button id="loadMore">Load more</button>

Ad
source: stackoverflow.com
Ad