Increasing quantity using plus and minus with jQuery doesn't work

Hi, I am creating a quantity input increment using jQuery, but have not yet managed to check my code and give me the correct guide.

SEE DEMO HERE

HTML

<div class="sp-quantity">
    <div class="sp-minus fff"> <a class="ddd" href="#">-</a></div>
    <div class="sp-input">
        <input type="text" class="quntity-input" value="1">
    </div>
    <div class="sp-plus fff"> <a class="ddd" href="#">+</a></div>
</div>

Js

$(".ddd").on("click", function () {

    var $button = $(this);
    var oldValue = $button.parent().find("input .quntity-input").val();

    if ($button.text() == "+") {
        var newVal = parseFloat(oldValue) + 1;
    } else {
        // Don't allow decrementing below zero
        if (oldValue > 0) {
            var newVal = parseFloat(oldValue) - 1;
        } else {
            newVal = 0;
        }
    }

    $button.parent().find("input .quntity-input").val(newVal);

});
+4
source share
3 answers

The selector to search for your input is incorrect. .quntity-inputis a child .sp-quantitythat is two levels above the button in the DOM, so you need to use closest()with a selector, not parent. Try the following:

$(".ddd").on("click", function () {

    var $button = $(this);
    var oldValue = $button.closest('.sp-quantity').find("input.quntity-input").val();

    if ($button.text() == "+") {
        var newVal = parseFloat(oldValue) + 1;
    } else {
        // Don't allow decrementing below zero
        if (oldValue > 0) {
            var newVal = parseFloat(oldValue) - 1;
        } else {
            newVal = 0;
        }
    }

    $button.closest('.sp-quantity').find("input.quntity-input").val(newVal);

});

Script example

You can also simplify your code:

$(".ddd").on("click", function() {
  var $button = $(this);
  var $input = $button.closest('.sp-quantity').find("input.quntity-input");

  $input.val(function(i, value) {
    return +value + (1 * +$button.data('multi'));
  });
});
.sp-quantity div { display: inline; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sp-quantity">
  <div class="sp-minus fff"><a class="ddd" href="#" data-multi="-1">-</a></div>
  <div class="sp-input">
    <input type="text" class="quntity-input" value="1" />
  </div>
  <div class="sp-plus fff"><a class="ddd" href="#" data-multi="1">+</a></div>
</div>
Run codeHide result
+9
source

Your input selector must be

$button.closest('.sp-quantity').find("input.quntity-input")

Demo: Fiddle

+3

, ?

    $('.sp-plus').on('click', function(){
        var oldVal = $('input').val();
        var newVal = (parseInt($('input').val(),10) +1);
      $('input').val(newVal);
    });

    $('.sp-minus').on('click', function(){
        var oldVal = $('input').val();
        if (oldVal > 0)
        {
            var newVal = (parseInt($('input').val(),10) -1);
        }
        else
        {
            newVal = 0;
        }
        var newVal = (parseInt($('input').val(),10) -1);
        $('input').val(newVal);
    });
+1

All Articles