How to use Jquery.mask for CPF with input using []

2

I'm trying to use Jquery.mask to insert a mask to a field with CPF but it does not work when the field has the following format:

  

$ ("# CPFPeser []")

The input looks like this:

<input type="text" id="CPFPessoa[]" name="CPFPessoa[]" size="15" class="form-control">
    $(document).ready(function () { 
        var $seuCampoCpf = $("#CPFPessoa[]");
        $seuCampoCpf.mask('000.000.000-00', {reverse: true});
    });

I need the field to have this format because I'm using a form that can be added to extra fields.

    
asked by anonymous 03.12.2018 / 18:03

1 answer

4

You only use brackets [] in name , not id .

But do not use id since you'll be able to repeat the field ( see this question about repeating id's ). You can use the name attribute as a selector:

var $seuCampoCpf = $("[name='CPFPessoa[]']");

Or change id by class :

<input type="text" class="CPFPessoa" name="CPFPessoa[]" size="15" class="form-control">

And the selector:

var $seuCampoCpf = $(".CPFPessoa");
    
03.12.2018 / 18:47