Function not running in Javascript

0

Javascript

function user(){
   input = document.getElementById("user").value;
   label = document.getElementById("tuser");
   if(label.style.display == "block"){
      if(input.length >= 1){
         label.innerHTML = "Usuário deve conter mais 5 caracteres.";
         if(input.length >= 5){
            label.style = "display:none";
         }
      }
   }
}

function pass(){
   input = document.getElementById("pass").value;
   label = document.getElementById("tpass");
   if(label.style.display == "block"){
      if(input.length >= 1){
         label.style = "display:none";
      }
   }
}

Both are not running, so it returns an error in the Chrome Console

(index):225 Uncaught TypeError: pass is not a function
    at HTMLInputElement.onkeypress ((index):225)

HTML (call function)

<input type="text" name="usuario" id="user" onkeypress="user()" autofocus maxlength="12" value="" class="form-control" placeholder="Seu Usuário">
<input type="password" name="senha" id="pass" onkeypress="pass()" maxlength="16" class="form-control" placeholder="Sua Senha">

Function will make change in

<font color="#ff0000" id="tuser" class="animation-slideUp inserted" style="display:none;">Insira seu usuário.</font>
<font color="#ff0000" id="tpass" class="animation-slideUp inserted" style="display:none;">Insira sua senha.</font>
  

I found it very strange that this happened, because the codes are   "correct" (I think). I ran them on the console and there were no errors.

    
asked by anonymous 09.11.2017 / 16:41

1 answer

-1

The order in which the JS script reference is made in HTML matters.

Inserts a console.log before the functions and sees itself on the browser's command line (F12 in principle / inspect element> Console). If it appears it is because you are calling the file, if it does not appear, it checks the paths.

"onkeypress" only makes sense if you have a loading script done, you have to open a giant function in the script with "document.onLoad" or "onready" and you start calling the functions in there. The browser running the DOM will load the JS scripts first before any concrete (aka, HTML and CSS) loading of your page, and if that happens you will not be able to call any function from there

$( document ).ready(function() {
     //funcoes ca para dentro a serem chamadas
    console.log( "ready!" ); //se este console já der, é porque te faltava isto do document
})

This code is in JQuery, so you might want to have a JQuery library to call before any other JS script

    
09.11.2017 / 17:59