jQuery totalCount hide if amount 0 or less

Viewed 25

I'm doing a simple count of child elements within a container. Subtracting 4 from the total amount and then displaying the count. This works fine.

The child divs will be created dynamically, so the amounts will change. How would I edit my jQuery to hide the count if the amount is 0 or any minus any amount?

Thanks

jQuery(document).ready(function(){
    var totalCount=$(".container .cell").length - 4;  
    $(".count-amount").html(totalCount);
}); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
  <div class="cell"></div>
</div>  


<div class="count-amount"></div>

1 Answers

You can use hide() to hide an element with jQuery.

jQuery(document).ready(function(){
    var totalCount=$(".container .cell").length - 4;
    if(totalCount < 1) {
      $('.count-amount').hide();
    }
    $(".count-amount").html(totalCount);
}); 
Related