Home Ask Login Register

Developers Planet

Your answer is one click away!

Jonas Dulay February 2016

How to change the value of check box when it is checked or unchecked

Hello guys here the problem. I want to perform a different calculation based on the check box.

<div>
 COD: <input type="checkbox" id="trigger" name="question" >
</div>

Here is the javascript for calculating the price of the item the problem is that i dont know how can i do the else if method

<script>
    $("#price,#quant,#shipment").keyup(function () {
      if(+myFunction3() =="" )
      {
        $('#demo').val(0);
      }
      else if($('#trigger')=="checked") //this is the problem
      {
        $('#demo').val($('#price').val() * $('#quant').val() ;
      }
      else
      {
      $('#demo').val($('#price').val() * $('#quant').val() + +myFunction3());
     }
  });
  </script>

Advance thank you guys.

Answers


Atif Tariq February 2016

Your script should be like this:

<script>
    $("#price, #quant, #shipment").keyup(function () {
      if(+myFunction3() =="" )
      {
        $('#demo').val(0);
      }
      else if ($('#trigger').is(':checked')) { //this is the problem

        $('#demo').val($('#price').val() * $('#quant').val() ;
      }
      else
      {
      $('#demo').val($('#price').val() * $('#quant').val() + +myFunction3());
     }
  });
  </script>


Pete Talks Web February 2016

Using jQuery you could do:

else if($('#trigger:checked').length > 0)

or

else if($('#trigger').is(':checked'))

If you want to re-run your calculation when a user checks or un-checks the checkbox, you could run the same code you run on the keyup function on:

$('#trigger').change(function() {
  ...
})


Sergei Kuleshov February 2016

Try this:

<script>
    $("#price,#quant,#shipment").keyup(function () {
      if(+myFunction3() =="" )
      {
        $('#demo').val(0);
        return;
      }

      var isCOD = $('#trigger').is(':checked');
      var val = $('#price').val() * $('#quant').val();

      if (isCOD) 
      {
         val += myFunction3();
      }

      $('#demo').val(val);
     }
  });
  </script>


Vignesh February 2016

Try this condition in else if:

else if($('input#trigger[type=checkbox]').is(":checked"))

Post Status

Asked in February 2016
Viewed 1,440 times
Voted 9
Answered 4 times

Search




Leave an answer


Quote of the day: live life