Focus on Input with JQuery

October 16, 2012

This is very simple and can also be done with HTML5 autofocus which I would recommend you use (if you can). If for some reason you need to control it with JavaScript, utalising jQuery’s .focus() event handler will get you out of trouble.

To focus on an input field with an id of search on page load, try:

$(document).ready(function() {
 $('#search').focus();
});