Home Javascript Javascript Auto Focus Form Element

Javascript Auto Focus Form Element

Published on May 5, 2008 by in Javascript

One of the main reasons I decided to have this blog is to create a reference place so I could easily find code or how to do things from time to time. Usually I have to sift through code trying to remember the last place I used something. From here on out I plan on posting code snippets as I run across them. Today I woke up to an email requesting if a signup form or member login form was opened, they wanted the cursor to automatically focus on the username or the first element of the form. To be honest, I didn't even know it could be done from a page open. So the first thing I did was open up Google and search until I find a way. I an not too ego minded to think I know everything. There is always someone that has found a better way.

Here is the JavaScript code:

Here is the XHTML part of the code:

For those that are a little new to javascript. The javascript should go in the document HEAD. The "element" can be changed to the id of the field you are working with.

 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
© Brainstorms of a Webdev