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

4.5k views Asked by At

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.

1

There are 1 answers

0
Chad On BEST ANSWER

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.