user14879075
user14879075

Reputation:

How can I add the the value of an "else" statement to it's previous "if"?

//the "#spring-products ul li:hidden" is a list of 24 items that show only 8 of them and other items are hidden

if ($("#spring-products ul li:hidden").length >= 8) {
// this is the span of the shown items
    $("#spring-shown-items").html($("#spring-products ul li").slice(0, 8).length + 8)
}
else {
    $("#spring-shown-items").html($("#spring-products ul li").slice(0, 8).length + $("#spring-products ul li:hidden").length)
}
<div class="load-more-btn">
  <div class="load-more-btn-total">
    <p>you have viewed&nbsp;</p>
    <span class="shown-items" id="spring-shown-items"></span>
    <p>&nbsp;of&nbsp;</p>
    <span class="total-itmes" id="spring-total-items"></span>
    <p>&nbsp;items</p>
  </div>
  <button>load more items</button>
</div>

The if statement checks if the length of the hidden items is >= 8, so if it is true it adds 8 to the current shown items, and if it is false it should add the rest of the hidden items length to the new value of the if statement... My problem is how can I add the 8 to the new value of "if" because it is now adding the rest of the hidden items to the first value not the new one. (thank you)

Upvotes: 0

Views: 50

Answers (1)

Brother Woodrow
Brother Woodrow

Reputation: 6372

If all you want is to show the number of visible li items, couldn't you just do:

$("#spring-shown-items").html($("#spring-products ul li:visible").length)

Then you wouldn't need the if/else at all.

Upvotes: 1

Related Questions