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 {
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 source
share