View Full Version : Focus on text field upon page load?
09-15-2005, 01:09 AM
How do I place the cursor in the first field of my form when the web page loads? I tried some simple code from ScriptSearch, but it didn't work for my page. Here's the script:
<BODY BGCOLOR="#FF0000" onload="self.focus();document.pw.lasso.CustomerNumber.focus()">
<FORM ACTION="pw.lasso" METHOD=POST>
<INPUT TYPE=text NAME=CustomerNumber VALUE="" SIZE=10>
Is it the "dot" in the action name? How to fix?
P.S. Even better to select the text!
09-15-2005, 01:26 AM
Sorry if it isn't correct, not too good with events >_<
09-15-2005, 01:36 AM
Thanks for the post, but where's the code?? The code box looks empty from here. Is it me, or did you forget to paste the code?
09-15-2005, 03:01 AM
it's you :rolleyes:
09-15-2005, 07:01 AM
I am in my home office now, and I see the code. It was me. Strange, I could see the code in other posts.
Being home, I'm not able to test this right now, but I'm wondering since "dots" are used to deliniate parts of the code, will the extra dot in my form name throw it off?
09-15-2005, 01:50 PM
The dot in the ACTION should be fine; the problem you might be having is that the form is not named (ie put name='myForm' into the FORM tag). Then onLoad='self.focus();document.myForm.CustomerNumber.focus();' should work.
FYI: I had also read someplace that IE sometimes acted strange when calling an immediate focus() on an object that didn't already have focus: the object would sort-of have focus. You wouldn't see the cursor anywhere, but hitting TAB would move focus to the next element in line. The work around for this problem was to delay the focus() call by a tenth of second, so the onLoad would look like:
onLoad='self.focus();setTimeout( 100,document.myForm.CustomerNumber.focus() );'
09-15-2005, 02:18 PM
Another possibility is:
var toFocus = document.getElementById("id_of_element");
09-15-2005, 02:49 PM
... If you do use the DOM's getElementById approach and need to also access the specific form (ie need to acces a field on a different window that's opened), use myForm.documents.getElementById('myElement')[.value] (getElementById returns the object with which other properties can be used. For example here the '.value' is optional, hense the 's (so don't include them!))
09-30-2005, 12:16 AM
First post ... found this post on google. GREAT source, thanks for the code pyth :)
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.