Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Count, count individually

I have counters in my cart and I'm struggling to get the controls to only target the specific counter I'm on. Instead, it's updating them all.

// Product Counter
DecreaseCount = function(){
  var count = $(".quantity__count").val();
  if (count > 0) count--;
  if (count == 0) count = 0;
  $(".quantity__count").val(count);
}

IncreaseCount = function(){
  var count = $('.quantity__count').val();
  if (count < 99) count++;
  if (count == 99) count = 99;
  $('.quantity__count').val(count);
}

// Does both
$('.quantity__decrement').click(function(){
  DecreaseCount();
});
$('.quantity__increment').click(function(){
  IncreaseCount();
});


// What I've tried
//$('.quantity__decrement').each(function(){
//  $(this).on("click", function(){
//    debugger;
//    var count = $(".quantity__count").val();
//    if (count > 0) count--;
//    if (count == 0) count = 0;
//    $(".quantity__count").val(count);
//  }); 
//});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="o-grid o-grid--no-gutter">  
  <div class="quantity o-grid__cell">
    <div class="quantity__decrement">-</div>
    <input type="num" class="quantity__count" value="0" />
    <div class="quantity__increment">+</div>
  </div>
</div>


<div class="o-grid o-grid--no-gutter">
  <div class="quantity o-grid__cell">
    <div class="quantity__decrement">-</div>
    <input type="num" class="quantity__count" value="0" />
    <div class="quantity__increment">+</div>
  </div>
</div>

I've tried using .closest, .siblings etc; I'm a bit stumped and I know this isn't a tricky thing to do.

Any help is appreciated!

like image 552
Sean Keenan Avatar asked May 24 '26 09:05

Sean Keenan


1 Answers

You can use the function siblings to select the quantity__count field and bind that element as the this context to provide a cleaner approach.

// Product Counter
DecreaseCount = function() {
              // +--- context this with the selected element.
              // |
              // v
  var count = $(this).val();
  if (count > 0) count--;
  if (count == 0) count = 0;
  $(this).val(count);
}

IncreaseCount = function() {
              // +--- context this with the selected element.
              // |
              // v
  var count = $(this).val();
  if (count < 99) count++;
  if (count == 99) count = 99;
  $(this).val(count);
}

// Does both
$('.quantity__decrement').click(function() {
  var target = $(this).siblings('[class="quantity__count"]').get(0);
                    // +--- Bind the function with the selected element.
                    // |
                    // v
  DecreaseCount.bind(target)();
});

$('.quantity__increment').click(function() {
  var target = $(this).siblings('[class="quantity__count"]').get(0);
                    // +--- Bind the function with the selected element.
                    // |
                    // v
  IncreaseCount.bind(target)();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="o-grid o-grid--no-gutter">
  <div class="quantity o-grid__cell">
    <div class="quantity__decrement">-</div>
    <input type="num" class="quantity__count" value="0" />
    <div class="quantity__increment">+</div>
  </div>
</div>


<div class="o-grid o-grid--no-gutter">
  <div class="quantity o-grid__cell">
    <div class="quantity__decrement">-</div>
    <input type="num" class="quantity__count" value="0" />
    <div class="quantity__increment">+</div>
  </div>
</div>
like image 117
Ele Avatar answered May 26 '26 21:05

Ele



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!