Oleksiï Nikonov
Oleksiï Nikonov

Reputation: 5588

jquery mask masked doesnt work. How set initial value to mask

I try to set initial value with jQuery Mask but the docs doesnt say correct way to it. I tried the next steps

    const options = {
        onKeyPress: (cep, e) => this.onChangeHandler(e),
        default: initialValue
    }
    $('#id').mask(maskPattern, options);

also

$('#id').mask(maskPattern, options);
$('#id').masked(initialValue);

But neither ways dont work.

I found a work around $('#id').val(initialValue).trigger('input'); at github issues and im going to share it in the answers, but i'm looking for the genuine way

Upvotes: 0

Views: 1572

Answers (1)

User863
User863

Reputation: 20039

masked() - Gets a masked value programmatically

You can set the value by

$('#id').val(initialValue).mask(maskPattern, options);

OR

$('#id').mask(maskPattern, options);
$('#id').val(initialValue).trigger('input');

More info

Upvotes: 1

Related Questions