Skip to main content
adicionou 3 caracteres ao conteúdo
Fonte Link
MarceloBoni
  • 6,4mil
  • 3
  • 30
  • 62
jQuery('#form-basic-informations').on('click',function(){
    var telMask = ["(99) 9999-99999", "(99) 99999-9999"];
    var tel = document.querySelector("input[attrname=telefone]");
    VMasker(tel).maskPattern(telMask[0]);
    tel.addEventListener("input", inputHandler.bind(undefined, telMask, 14), false);

    var datMask = ["99/99/9999"];
    var dat = document.querySelector("input[attrname=data]""input[attrname=dataNas]");
    VMasker(dat).maskPattern(datMask[0]);
    dat.addEventListener("input", inputHandler.bind(undefined, datMask, 14), false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet" />

<span><a href="" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#modalInfPessoais">Alterar informações pessoais</a></span>

<!-- Modal Inf Pessoais -->
<div class="modal" id="modalInfPessoais" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Teste</h4>
      </div>
      <form class="form-horizontal" id="form-basic-informations" action="#">
        <div class="modal-body">
          <fieldset>
            <div class="form-group">
              <div class="col-md-12">
                <input id="nome" name="nome" required type="text" placeholder="Nome" class="form-control input-md">
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-12">
                <input id="dataNas" name="dataNas" type="text" placeholder="Data de nascimento" class="form-control input-md">
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-12">
                <input id="telefone" name="telefone" type="text" placeholder="Telefone" class="form-control input-md">
              </div>
            </div>
          </fieldset>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
          <button type="submit" class="btn btn-primary" name="edit-basic-informations" id="edit-basic-informations">Ok</button>
        </div>
      </form>
    </div>
  </div>
</div>
jQuery('#form-basic-informations').on('click',function(){
    var telMask = ["(99) 9999-99999", "(99) 99999-9999"];
    var tel = document.querySelector("input[attrname=telefone]");
    VMasker(tel).maskPattern(telMask[0]);
    tel.addEventListener("input", inputHandler.bind(undefined, telMask, 14), false);

    var datMask = ["99/99/9999"];
    var dat = document.querySelector("input[attrname=data]");
    VMasker(dat).maskPattern(datMask[0]);
    dat.addEventListener("input", inputHandler.bind(undefined, datMask, 14), false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet" />

<span><a href="" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#modalInfPessoais">Alterar informações pessoais</a></span>

<!-- Modal Inf Pessoais -->
<div class="modal" id="modalInfPessoais" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Teste</h4>
      </div>
      <form class="form-horizontal" id="form-basic-informations" action="#">
        <div class="modal-body">
          <fieldset>
            <div class="form-group">
              <div class="col-md-12">
                <input id="nome" name="nome" required type="text" placeholder="Nome" class="form-control input-md">
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-12">
                <input id="dataNas" name="dataNas" type="text" placeholder="Data de nascimento" class="form-control input-md">
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-12">
                <input id="telefone" name="telefone" type="text" placeholder="Telefone" class="form-control input-md">
              </div>
            </div>
          </fieldset>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
          <button type="submit" class="btn btn-primary" name="edit-basic-informations" id="edit-basic-informations">Ok</button>
        </div>
      </form>
    </div>
  </div>
</div>
jQuery('#form-basic-informations').on('click',function(){
    var telMask = ["(99) 9999-99999", "(99) 99999-9999"];
    var tel = document.querySelector("input[attrname=telefone]");
    VMasker(tel).maskPattern(telMask[0]);
    tel.addEventListener("input", inputHandler.bind(undefined, telMask, 14), false);

    var datMask = ["99/99/9999"];
    var dat = document.querySelector("input[attrname=dataNas]");
    VMasker(dat).maskPattern(datMask[0]);
    dat.addEventListener("input", inputHandler.bind(undefined, datMask, 14), false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet" />

<span><a href="" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#modalInfPessoais">Alterar informações pessoais</a></span>

<!-- Modal Inf Pessoais -->
<div class="modal" id="modalInfPessoais" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Teste</h4>
      </div>
      <form class="form-horizontal" id="form-basic-informations" action="#">
        <div class="modal-body">
          <fieldset>
            <div class="form-group">
              <div class="col-md-12">
                <input id="nome" name="nome" required type="text" placeholder="Nome" class="form-control input-md">
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-12">
                <input id="dataNas" name="dataNas" type="text" placeholder="Data de nascimento" class="form-control input-md">
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-12">
                <input id="telefone" name="telefone" type="text" placeholder="Telefone" class="form-control input-md">
              </div>
            </div>
          </fieldset>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
          <button type="submit" class="btn btn-primary" name="edit-basic-informations" id="edit-basic-informations">Ok</button>
        </div>
      </form>
    </div>
  </div>
</div>
Fonte Link
MarceloBoni
  • 6,4mil
  • 3
  • 30
  • 62

Problema Error: VanillaMasker: There is no element to bind

Estou usando a lib VanillaMasker para validação de um campo telefone e data, só que esses elementos estão dentro de um modal bootstrap Tentei jogar a função pra dentro de uma função do tipo:

jQuery('#form-basic-informations').on('click',function(){

Mas ainda não está funcionando... retorna o erro no console:

Error: VanillaMasker: There is no element to bind.

jQuery('#form-basic-informations').on('click',function(){
    var telMask = ["(99) 9999-99999", "(99) 99999-9999"];
    var tel = document.querySelector("input[attrname=telefone]");
    VMasker(tel).maskPattern(telMask[0]);
    tel.addEventListener("input", inputHandler.bind(undefined, telMask, 14), false);

    var datMask = ["99/99/9999"];
    var dat = document.querySelector("input[attrname=data]");
    VMasker(dat).maskPattern(datMask[0]);
    dat.addEventListener("input", inputHandler.bind(undefined, datMask, 14), false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet" />

<span><a href="" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#modalInfPessoais">Alterar informações pessoais</a></span>

<!-- Modal Inf Pessoais -->
<div class="modal" id="modalInfPessoais" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Teste</h4>
      </div>
      <form class="form-horizontal" id="form-basic-informations" action="#">
        <div class="modal-body">
          <fieldset>
            <div class="form-group">
              <div class="col-md-12">
                <input id="nome" name="nome" required type="text" placeholder="Nome" class="form-control input-md">
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-12">
                <input id="dataNas" name="dataNas" type="text" placeholder="Data de nascimento" class="form-control input-md">
              </div>
            </div>
            <div class="form-group">
              <div class="col-md-12">
                <input id="telefone" name="telefone" type="text" placeholder="Telefone" class="form-control input-md">
              </div>
            </div>
          </fieldset>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
          <button type="submit" class="btn btn-primary" name="edit-basic-informations" id="edit-basic-informations">Ok</button>
        </div>
      </form>
    </div>
  </div>
</div>

Esse mesmo código funciona se o elemento não estiver dentro do modal.