The problem I really do not know where it comes from, and it's quite simple, but I do not know how to solve it, the issue is that I have a form that is to be able to enter X site as a user, then it has its respective field of user and password the topic is super simple, if the user is empty, he must skip the modal to indicate to the user that he has an empty field and he must fill it out, the issue is that when I reload and try the script, the first attempt does not work, but the second attempt and all those that precede it if it works and I do not know why it happens
JS Code
window.addEventListener('load', () => {
var form = document.getElementById("form");
var user = document.getElementById("user").value;
var pass = document.getElementById("pass").value;
form.addEventListener('submit', () => {
alert("Tomo los datos" + form);
if (user == "") {
$('#view_error').modal('show');
} else {
$('#view_loged').modal('show');
//alert("logeado");
}
});
});
Código de HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display modal</title>
<script src="js/modal.js"></script>
<link rel="stylesheet" href="css/Login-Form-Dark.css">
<link rel="stylesheet" href="css/styles.css">
<link rel="stylesheet" href="css/sett.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
</head>
<body>
<!-- Inicio modal Error -->
<div class="modal fade" id="view_error" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Error al inciar sesión</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
<!-- Cierre Modal Error -->
<div class="login-dark">
<form action="#" id="form">
<h2 class="sr-only">Login Form</h2>
<div class="illustration"><i class="icon ion-ios-locked-outline"></i></div>
<div class="form-group"><input class="form-control" type="text" name="user" id="user" placeholder="Usuario"></div>
<div class="form-group"><input class="form-control" type="password" name="pass" id="pass" placeholder="Contraseña"></div>
<div class="form-group"><button class="btn btn-primary btn-block" type="submit" data-toggle="modal" data-target="#view_error">Log In</button></div><a href="#" class="forgot">Forgot your email or password?</a>
</form>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.bundle.min.js"></script>
</body>
</html>