Jquery how to set a value in a field with CPF Mask?

Asked

Viewed 685 times

1

I have an input using an example CPF mask :

$("#cpfDependente").mask('000.000.000-00');

and I’m trying to set a value in the input this way

$("#cpfDependente").val(dependente.cpf);

It correctly arrow the problem is that the mask some what the solution of this? I thank you in advance !

2 answers

3

Reverse the order.

Add this command:

$("#cpfDependente").val(dependente.cpf);

then add the mask:

$("#cpfDependente").mask('000.000.000-00');
  • Just to top it off, look at one example in jsfiddle. +1 for the answer.

  • I’ve tried that before but it wasn’t !

  • You should add the mask in $( Document ).ready(Function() { //Here the mask });

0


The solution I found was to add the property data-mask="000.000.000-00" masking in HTML itself:

<input class="form-control" id="cpfDependente" name="cpfDependente" type="text" data-mask="000.000.000-00"/>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.