Inputmask icon indicating copy to clipboard operation
Inputmask copied to clipboard

Can't clear field by updating js object when using knockout and inputmask

Open adam-resdiary opened this issue 11 years ago • 4 comments

I'm having a bit of a problem. We use knockout for databinding, and have created a simple knockout binding for using it:

ko.bindingHandlers.inputmask = {
    init: function (element, valueAccessor) {
        var format = ko.utils.unwrapObservable(valueAccessor());

        $(element).inputmask(format);
    }
};

The binding just lets us apply a mask to inputs like the following:

<input data-bind="value: amount, inputMask: { alias: 'numeric', ... }"></input>

The trouble I'm having is that if I clear a field by setting its value to null in the knockout ViewModel, inputmask seems to store the value behind the scenes, and re-enters it into the input when I start typing in a new value.

Here's a fiddle demonstrating it: http://codepen.io/anon/pen/Wbwjgb

I couldn't see a CDN version of the library, so I ended up pasting the minified version into the javascript pane. Search for "End of jquery.inputmask.min.js" to see where my code starts.

Steps to reproduce:

  1. Enter "12.99" into the field.
  2. Click on the Clear button.
  3. Enter "1" into the field.

Expected result: the field should have "1" in it. Actual result: the field ends up with "112.99" in it.

I had a quick look at the code to try to figure out if it was a bug in our binding or something, but I wasn't really getting anywhere, which is why I'm raising this issue.

Lastly - jsfiddle is down at the moment because of a DDOS attack, and codepen seems to be running really slowly. I managed to create the example in it, but I'm not sure if you'll have trouble accessing it just now.

adam-resdiary avatar Dec 12 '14 13:12 adam-resdiary

In firefox your binding will work correctly because firefox allows for patching the native value property. Use jquery.val to clear the value or trigger thz setval event after clearing the value with the value property.

RobinHerbots avatar Dec 14 '14 10:12 RobinHerbots

Thanks for the reply. Just so I can figure out how to sort this properly, is this an issue with the inputmask library, or is it a Chrome or knockout problem?

I can't easily use jquery.val to clear the value because the point of using knockout is that I don't have to access the input itself from my VM - I just update a Javascript object and knockout deals with setting its value.

I'll take a look and see if I can alter the custom inputmask binding, but I just want to know if that's going to be a workaround or the proper solution.

adam-resdiary avatar Dec 15 '14 09:12 adam-resdiary

@adam-resdiary ,

The problem is when you set the value through the native value property AND it is not allowed by the browser to patch that property, the inputmask isn't aware of the change. If you cannot set the value by jquery.val the best solution is to raise the setvalue.inputmask event.

RobinHerbots avatar Dec 15 '14 14:12 RobinHerbots

Ok. Thanks.

adam-resdiary avatar Dec 15 '14 15:12 adam-resdiary