0


what is the fastest (the really fastest) way to set the focus on a textfield?

My page looks like

<html>
 <head> ... </head>

  <body>

    ... content ...  

    <input type="text" id="ct_one" name="ct_pet[]" value="" />

    ... content ...

   <script> ... load jquery and jquery_ui ... </script>

  </body>

</html>

Thanks in advance!
Peter

clairesuzy
  • 27,296
  • 8
  • 54
  • 52
Peter
  • 11,413
  • 31
  • 100
  • 152

3 Answers3

2

You can put a script right after the element, that sets focus to it. That would set focus at the earliest possible moment, i.e. right after the element is created.

<input type="text" id="ct_one" name="ct_pet[]" value="" />

<script type="text/javascript">
document.getElementById('ct_one').focus();
</script>
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Assign value 0 to attribute tabindex for your input field.

http://www.w3.org/TR/html4/interact/forms.html#adef-tabindex

AlexD
  • 914
  • 7
  • 15
  • That will only place it first in the tab order, it won't focus it. – Guffa Apr 10 '11 at 09:24
  • Yes, I was wrong. It doesn't get focus and with value "0" field won't even be first in tab order. Instead it will be last in order of elements with "tabindex" set. I should had checked it before posting. – AlexD Apr 10 '11 at 10:58
0

the accepted answer here seems to be "the really fastest" :)

Set input field focus on start typing

JSFiddle example

Community
  • 1
  • 1
clairesuzy
  • 27,296
  • 8
  • 54
  • 52