Kelijsto
Kelijsto

Reputation: 73

How to decrease a number value with a checkbox?

When i use this script to decrease a number value with a checkbox, my counter return from 0.

$(".removenumber").click(function() {
                var currValue = $(".numberresult").data("value");
                currValue = parseInt(currValue) ? parseInt(currValue) : 0;
                if (currValue > 0) {
                    $(".numberresult").progressbar({
                        value: currValue - 1
                    }).data("value", currValue - 1);
                    $(".totalnumber").html((currValue - 1));
                }
            });

This is the checkbox imput to decrease the number value.

<input type="checkbox" class="custom-control-input removenumber" id="LoginModalSwitch">

This is the script to increase the number with a button

$(".addnumber").click(function() {
            var currValue = $(".numberresult").data("value");
            currValue = parseInt(currValue) ? parseInt(currValue) : 0;
            if (currValue < 4) {
                $(".numberresult").progressbar({
                    value: currValue + 1
                }).data("value", currValue + 1);
                $(".totalnumber").html((currValue + 1));
            }

This is the button to increase the number value.

<button class="btn btn-primary addnumber" type="button" data-dismiss="modal">Now</button>

Upvotes: 2

Views: 135

Answers (1)

Ghanshyam Singh
Ghanshyam Singh

Reputation: 1381

Something like this may work:-

$(document).on("change", ".removenumber",function(e) {
  if(!$(this).is(":checked")) {   // if :checkbox isn't checked than decrement.
     var currValue = $(".numberresult").data("value");
     currValue = parseInt(currValue) ? parseInt(currValue) : 0;
     if (currValue > 0) {
       $(".numberresult").progressbar({
          value: currValue - 1
        }).data("value", currValue - 1);
       $(".totalnumber").html((currValue - 1));
     }
   }
});

Upvotes: 2

Related Questions