Welcome to my blog!

Here I try to keep useful information about IT, mostly related to Web development and Linux stuff. Any comments or feedback that you might have will be much appreciated!

Thanks,
Tomi

HTML / JavaScript: Focus user on first available field

Filed Under (JavaScript) by admin on 22-01-2007

So far I haven’t figured out where is the user’s caret when entering an HTML page with multiple fields.

With the following code (placed right before the closing HTML tag), the page will load and focus the first text or text area field on the page.

The beauty of the code is that it won't matter the name of the form or field, giving enough flexibility to use the same JavaScript code on all the pages that have fields in your application.

Notes:
- Different types of fields can be used to receive focus changing the if condition.
- Change the form's index to the one containing the field that should receive the focus, it currently checks the fields on the first form (index starting at 0).

Leave a Reply

You must be logged in to post a comment.