Timo Denk
Timo Denk

Reputation: 573

focus input before DOM is load

How can I focus an text input when it is visible which is probably before the whole DOM is ready? I only have to possibility to run a script before the DOM started loading. The reason I want to do this is that the user wants to start typing until the input is visible and loading the DOM takes a while.

I would like to do it with jQuery.

Greetings!

Upvotes: 0

Views: 105

Answers (2)

Super Hornet
Super Hornet

Reputation: 2907

try something like this:

 <input id="username" type="text" name="username"/>
 <script>
   $("#username").focus();
 </script>

after declaring DOM in html write your scripts

supported in major Browsers

Upvotes: 1

Quentin
Quentin

Reputation: 944429

No need to involve JavaScript. Just use the autofocus attribute.

<input name="foo" autofocus>

Upvotes: 2

Related Questions