Search code examples
javascriptjqueryonchangeautonumeric.js

jQuery - autoNumeric plugin, fire .change() automatically from .autoNumeric('set', value)


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.


Solution

  • 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.