Issue Error: Vanillamasker: There is no element to bind

Asked

Viewed 963 times

0

I’m using the lib Vanillamasker for validation of a telephone field and date, only that these elements are within a modal bootstrap I tried to turn the function into a function like:

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

But still not working... returns the error on the 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=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>

This same code works if the element is not inside the modal.

  • 1

    Your phone input selection is incorrect; try it this way. document.querySelector('input[name="telefone"]');

  • The phone worked, but another error msg appeared on the console: << Referenceerror: inputHandler is not defined >>

  • Try applying more filters to the selection of the element, ('modal modal-body form-group input[name="telefone"]')

  • It didn’t work if I take the x.addEventListener("input", inputHandler.bind(undefined, datMask, 14), false); it works, now I was intrigued

  • forget, I figured out what it was, it was missing function inputHandler, my mistake, put your first answer that I accept it

1 answer

1


Your line

var tel = document.querySelector("input[attrname=telefone]");

this with the wrong syntax, do as follows.

var tel = document.querySelector('input[name="telefone"]');

Browser other questions tagged

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