0

I'm using the autoNumeric jQuery plugin. I want to have the .autoNumeric('set', value) method call the .change() event automatically.

I have the following code:

$(document).ready(function ($) {

    $("#baseCost").change(function() {
        var total = ...; // calculation removed for code brevity
        $("#totalCost").autoNumeric('set', total);
    });

    $("#totalCost").change(function() {
        // this code does not fire when the set above is called
    });

});

How would I accomplish this?

Update: In the autoNumeric.js file, I found this (within set):

if ($input) {
    return $this.val(value);
}

Given that in my case $input is set to true, I don't understand why this .val() is not firing the .change() on my page.

MBaas
  • 7,248
  • 6
  • 44
  • 61
Chad
  • 1,531
  • 3
  • 20
  • 46

1 Answers1

2

I did not realize that .val() does not trigger .change() by default. For my question specifally, the following does the trick in autoNumeric.js:

if ($input) {
    if ($this.val(value)) {
        return $this.trigger("change");
    }
    return false;
}

For more information, see this answer.

Community
  • 1
  • 1
Chad
  • 1,531
  • 3
  • 20
  • 46