I have a login form in a modal in bootstrap 4, and I want autofocus in the fields email and password .
<form action="proceso.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Correo electrónico">
</div>
<div class="form-group">
<input type="password" class="form-control" id="password" name="password" placeholder="Contraseña">
</div>
<button type="submit" class="btn btn-primary btn-block">Aceptar</button>
</form>
Reading the bootstrap documentation. To put autofocus in the field email I use this, and it works:
<script>
$('#login').on('shown.bs.modal', function () {
$('#email').trigger('focus')
})
</script>
Now I want the focus to go to the password field after ENTER, for this I add the following:
<script>
$('#login').on('shown.bs.modal', function () {
$('#password').trigger('focus')
})
</script>
But when loading the page the focus appears in the second field. What is the problem?