How to automatically set the autofocus attribute to HTML input by Javascript?

Asked

Viewed 1,958 times

2

I need to automatically set autofocus to the input contained in a modal whenever I open it. What Javascript function can I use for this?

<input type="email" class="form-control" placeholder="Informe o e-mail" value="{{cliente.getEmail()}}" name="email" id="emailCliente" required>

  • how are you opening this modal? will depend on it!

  • By clicking a button it opens with input. I need this input to automatically receive autofocus.

  • Put every example in your question we have no way of knowing what you did there!

2 answers

3


By the class that is in the input form-control it seems to me that you use Bootstrap. In this part in the documentation they indicate that you use the event .on('shown.bs.modal', function () { } to interact with the modal, so in your case would look like this. See the official documentation https://getbootstrap.com/docs/4.0/components/modal/

$('#myModal').on('shown.bs.modal', function () {
    $('#myInput').focus();
})

See the model working.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>Page Title</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" type="text/css" media="screen" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<style>
	
</style>
</head>
<body>
	
		<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
  Abrir modal com input focado
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <input type="text" value="sem foco" id="">
        <input type="text" value="input com autofocus" id="myInput">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>


	
	<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>

	<script>
		$('#myModal').on('shown.bs.modal', function () {
			$('#myInput').focus();
		})
		</script>
</body>
</html>

  • 1

    It worked. Thank you!

  • @Victormoraeslegal who worked there!

0

You can do this by adding to the event that opens the modal

function abrirModal(){
    var element = document.getElementsByName("email")[0];
    element.focus();
}

I made some corrections to the code and you can see it working here https://jsfiddle.net/c25gvj9b/

Browser other questions tagged

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