Home » Jquery » javascript – onchange function not running for some reason

javascript – onchange function not running for some reason

Posted by: admin February 22, 2020 Leave a comment

Questions:

I have a read-only textbox (sum) and whenever its value changes, I want a second read-only textbox (totalcost) to update its value.

Here’s what I have, but for some reason it’s not working. All of the references are correct, am I missing something basic?

jQuery(function($) {
  $('input[name="totalCPC"]').on('change', function() {
    var pc = parseFloat($('input[name="percentdiscount"]:checked').val());
    var cpc = parseFloat($('input[id="sum"]').val());
    var noc = parseFloat($('input[id="numberofclickstextbox"]').val());
    $('#totalcost').val(function() {
      return ((noc * cpc) *pc).toFixed(2);
      });
  });
});
How to&Answer:

You need to trigger change event

$('#totalcost').val(function() {
  return ((noc * cpc) *pc).toFixed(2);
}).trigger('change');