jqueryjquery-mask

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


Solution

  • 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