How to focus on a form input text field on page load using jQuery?

chris picture chris · Oct 20, 2009 · Viewed 402.9k times · Source

This is probably very simple, but could somebody tell me how to get the cursor blinking on a text box on page load?

Answer

DOK picture DOK · Oct 20, 2009

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();