rodrix rodrix - 1 year ago 67
jQuery Question

substract 1 from total if check box is checked

say you have a checkbox and beside it u show any numbers be it 10 and when user clicks on the checkbox then that 10 becomes 9 but if unchecks then it again becomes 10

<input type="checkbox" id="credits" name="credits" <?php echo $checked;?> /> (10) Credit

the value of credit would be fetched from database

thanks in advance for your precious time and help.

Answer Source

$(function () {
  $('#credits').change(function () {
    var currentValue = parseInt($('#credit-amount').text());
    var newValue = currentValue + ($(this).prop('checked') ? -1 : 1);
<script src=""></script>
<input type="checkbox" id="credits" name="credits" /> (<span id="credit-amount">10</span>) Credit

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download