-1
Good night!
I’m studying JS and I came across a problem apparently common but I’m not able to solve.
I have the JS, where I create a variable for the user and password, and put in the end the "value" to get the value typed by the user. But always returns me the same error of "Cannot read Property 'value' of null".
I have tested putting the "value" in my IF, in the test function (even putting values, setInterval always brings as null).
I believe it is a problem concerning the page loading order, but I do not know how to solve the problem
var button = document.querySelector("#logar");
var user = document.getElementById("#usuario").value;
var senha = document.getElementById("#senha").value;
button.onclick = function confirmaLiberacao() {
if (validarUser(user,senha) == true) {
window.open('liberado.html');
} else
window.open('negado.html');
};
function teste() {
console.log(user,senha);
};
setInterval(teste,3000);
<!DOCTYPE html>
<html>
<head>
<title>Teste</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style/style.css">
<link href="https://fonts.googleapis.com/css?family=Inter|Roboto+Condensed&display=swap" rel="stylesheet">
</head>
<body>
<header></header>
<main>
<section class="login">
<h2>Faça o login ou cadastra-se</h2>
<form action="/">
<input type="text" id="usuario" autocomplete="name" placeholder="Usuário ou e-mail" required="required">
<input type="password" autocomplete="current-password" id="senha" placeholder="Senha" required="required">
<button class="botao" id="logar">Login</button>
<button class="botao" id="cadastrar">Cadastrar</button>
</form>
</section>
</main>
<footer></footer>
<script type="text/javascript" src="../Model/model.js"></script>
<script type="text/javascript" src="../Controller/controller.js" aynsc></script>
</body>
</html>
On the lines
var user = document.getElementById("#usuario").value;
andvar senha = document.getElementById("#senha").value;
withdraw the#
parameters. Or if you want to keep the#
change the methodgetElementById()
forquerySelector()
.– Augusto Vasques