Focus First Input Field

Here’s a handy little snippet to automatically put the cursor in the first input field on the page. You can override it by adding a class of “nofocus” to any element and/or list the elements in the array. Nice for login pages.


if( !$(':input:visible:enabled:first').hasClass('nofocus') &&
		$.inArray( $(':input:visible:enabled:first').attr('id'),
				  ['element_to_exclude', 'another_element_to_exclude']
		        ) == -1
  ){
	$(':input:visible:enabled:first').focus();
}
This entry was posted in Code Snippets and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>