I have a cart blade page on which products are shown with ajax add to cart action. I want to get the value of the input with name=quantity when a button is clicked. I want to do this with the jquery prev() function as there is right the code but it can’t show any alert. Maybe it can’t identify the class. So how can I do that? I have
//Update Cart Items
$(document).on("click", ".btnItemUpdate", function() {
if ($(this).hasClass("qtyMinus")) {
var quantity = $(this).prev().val();
console.log(quantity);
return false;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cart_quantity_button">
<input class="cart_quantity_input" type="text" name="quantity" value="0" autocomplete="off" size="1" disabled>
<button class="btnItemUpdate qtyMinus" type="button" data-cartid="{{$item['id']}}"> - </button>
<button class="btnItemUpdate qtyPlus" type="button" data-cartid="{{$item['id']}}"> + </button>
</div>
2
Answers
Try this
Above code will seek the closest div which has class
cart_quantity_button
from.btnItemUpdate
and then will find an input which hasquantity
as its name , don’t forget to parse quantity as integer usingparseInt()