This is probably very simple, but could somebody tell me how to get the cursor blinking on a text box on page load?
12 Answers
Set focus on the first text field:
$("input:text:visible:first").focus();
This also does the first text field, but you can change the [0] to another index:
$('input[@type="text"]')[0].focus();
Or, you can use the ID:
$("#someTextBox").focus();

- 32,337
- 7
- 60
- 92
-
2If using a dialog please see this answer if the above is not working http://stackoverflow.com/a/20629541/966609 – Matt Canty Aug 04 '14 at 15:46
-
1`$('input[type="text"]').get(0).focus();` ...worked for me – Rav Aug 28 '16 at 09:53
-
3`.focus()` was depricated in 3.3, for newer versions use `.trigger('focus');` – ricks Feb 23 '22 at 15:16
You can use HTML5 autofocus
for this. You don't need jQuery or other JavaScript.
<input type="text" name="some_field" autofocus>
Note this will not work on IE9 and lower.

- 110,530
- 99
- 389
- 494

- 2,478
- 5
- 22
- 29
-
autofocus does not work in IE11 either https://stackoverflow.com/questions/34068302/autofocus-attribute-on-input-field-not-working-in-ie-11 – BA TabNabber Jan 09 '18 at 23:03
-
2This works for my scenario but the selected answer did not for some reason. – Vishnu Y S Aug 31 '18 at 07:41
-
This is great and also shorter, even if a single letter `id` was used! :P – Andreas Aug 25 '20 at 09:58
Sure:
<head>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#myTextBox").focus();
});
</script>
</head>
<body>
<input type="text" id="myTextBox">
</body>

- 9,506
- 9
- 43
- 61
-
4
-
1@AliSheikhpour script is within the dom ready wrapper so it doesn't matter that it's before the html element, but it shouldn't be in head anyway. – Popnoodles Jun 19 '19 at 12:21
Why is everybody using jQuery for something simple as this.
<body OnLoad="document.myform.mytextfield.focus();">

- 1,178
- 1
- 13
- 18
Think about your user interface before you do this. I assume (though none of the answers has said so) that you'll be doing this when the document loads using jQuery's ready()
function. If a user has already focussed on a different element before the document has loaded (which is perfectly possible) then it's extremely irritating for them to have the focus stolen away.
You could check for this by adding onfocus
attributes in each of your <input>
elements to record whether the user has already focussed on a form field and then not stealing the focus if they have:
var anyFieldReceivedFocus = false;
function fieldReceivedFocus() {
anyFieldReceivedFocus = true;
}
function focusFirstField() {
if (!anyFieldReceivedFocus) {
// Do jQuery focus stuff
}
}
<input type="text" onfocus="fieldReceivedFocus()" name="one">
<input type="text" onfocus="fieldReceivedFocus()" name="two">

- 318,141
- 75
- 454
- 536
-
2Your perspective is not only technically correct, but your consideration for the best UX possible is exquisite. Bravo! – Folusho Oladipo Feb 06 '17 at 10:42
HTML:
<input id="search" size="10" />
jQuery:
$("#search").focus();

- 29,308
- 20
- 68
- 109
-
1if using html5, just add `autofocus` attribute to the input element? – Adarsh Madrecha Feb 13 '18 at 15:40
-
How is that attribute used, and will it become the focused element as soon as its parent form appears? – Khom Nazid Jun 08 '19 at 05:40
Sorry for bumping an old question. I found this via google.
Its also worth noting that its possible to use more than one selector, thus you can target any form element, and not just one specific type.
eg.
$('#myform input,#myform textarea').first().focus();
This will focus the first input or textarea it finds, and of course you can add other selectors into the mix as well. Handy if you can't be certain of a specific element type being first, or if you want something a bit general/reusable.

- 451
- 4
- 8
This is what I prefer to use:
<script type="text/javascript">
$(document).ready(function () {
$("#fieldID").focus();
});
</script>

- 143
- 1
- 5
-
3It's bad way to do things, to focus on an on document load, just use the `autofocus` attribute provided by HTML5 – OverCoder May 24 '16 at 10:43
place after input
<script type="text/javascript">document.formname.inputname.focus();</script>

- 31
- 1
The line $('#myTextBox').focus()
alone won't put the cursor in the text box, instead use:
$('#myTextBox:text:visible:first').focus();

- 5,263
- 2
- 38
- 42
The Simple and easiest way to achieve this
$('#button').on('click', function () {
$('.form-group input[type="text"]').attr('autofocus', 'true');
});

- 980
- 3
- 17
- 37