Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a field autofocus?

I need this field to be focused when the user opens the page. I don't know if it changes anything but it's inside a modal window I load from a PHP file.

Is there an easy way to do it?

like image 306
lisovaccaro Avatar asked Jan 13 '11 02:01

lisovaccaro


People also ask

What is autofocus for input field?

Definition and Usage The autofocus attribute is a boolean attribute. When present, it specifies that an <input> element should automatically get focus when the page loads.

How do you focus on a particular field in React?

We used the focus() method to focus the input field in the handleClick function. The current property on the ref gives us access to the input element, so calling ref. current. focus() sets the focus on the input.

How do you focus a text field in HTML?

Input Text focus() Method The focus() method is used to give focus to a text field. Tip: Use the blur() method to remove focus from a text field.


1 Answers

Using JavaScript, you can achieve this:

document.onload = function() {  
  document.getElementById("question-box-0").focus();
}
like image 110
Michael Irigoyen Avatar answered Sep 17 '22 12:09

Michael Irigoyen