How to enable autofocus with jquery in a div?

Asked

Viewed 1,847 times

0

How to enable autofocus with jquery in a div? I want to autofocus an input as soon as the user does such an action...

  • 1

    The autofocus would be to give focus when loading the page. If it is after an action, just you give the focus in the field by doing $("seu campo").focus()

  • 2

    THAT DELICIAAAAAAAAAAAAAAAAAAAAAAAAAAAAA VALEUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU

  • 1

    As a complement to what has already been answered by @Andersoncarloswoss and although not focusing on the tag, you can do the same only with html using the attribute autofocus, which will work like Focus when opening the page.

1 answer

6

Just use the function focus jQuery when you want to focus on the field. For example, focus on a text field by clicking the button:

$(() => {
  $("#btn").on("click", event => {
    $("#nome").focus();
    console.log("O campo está em foco.");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="nome" type="text">
<button id="btn">Foco</button>

Browser other questions tagged

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