user3932810 user3932810 - 5 months ago 13
jQuery Question

Show the button when input value reaches 0

I have input box where in it increases the product quantity by clicking on +/- icons.
By default there will be an "Add" button, clicking on that it enables +/- input filed and hide the "Add" button. Now when the input value reaches

0
I need to show the "Add" button and hide the input field.

Here is my script used

$('button').on('click',function(){
$(".incrmentrs").show();
$(this).hide();
});
$(function () {

$('.add').on('click',function(){
var $qty=$(this).parent().find('.qty');
var currentVal = parseInt($qty.val());
if (!isNaN(currentVal)) {
$qty.val(currentVal + 1);
}
});
$('.minus').on('click',function(){
var $qty=$(this).parent().find('.qty');
var currentVal = parseInt($qty.val());
if (!isNaN(currentVal) && currentVal > 0) {
$qty.val(currentVal - 1);
}
});
});


Demo

Answer

Try this:

https://jsfiddle.net/ovt6f7ev/

Change minus click event code like this:

$('.minus').on('click',function(){
            var $qty=$(this).parent().find('.qty');
            var currentVal = parseInt($qty.val());
            console.log(currentVal)
            if (!isNaN(currentVal) && currentVal > 1) { // check if value is 1 or more then minus value
                $qty.val(currentVal - 1);
            } else { // else show button
               $(".incrmentrs").hide();
               $("button").show(); 
            }
 });