Giving a Field the Focus


document.forms[0].elements["textfield"].focus(); 

Most search websites like Google have a convenient feature: After the page has been loaded, the cursor is already in the search field. This is quite easily done by the preceding code; the focus() method gives a form field the focus. A full listing is available in the file focus.html.

Warning

Note that the larger your page is and the more form fields it has, the more inconvenient this feature may get. Imagine a slowly loading page. While the page is loading, the user is probably already typing in another field. After the page has been fully loaded, the preceding code sets the focus to the designated field, eventually stealing the focus from the field the user is currently working on.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net