Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Focus on text field upon page load?

    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:
    </HEAD>
    <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?

    Thanks,
    Ken
    P.S. Even better to select the text!
    Last edited by SoCalMacDude; 09-15-2005 at 01:53 AM.

  • #2
    Regular Coder
    Join Date
    May 2005
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script type="text/javascript">
    document.body.onload=function(){
    this.focus()
    document.myform.focus()
    }
    </script>
    Sorry if it isn't correct, not too good with events >_<

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks, but . . .

    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?
    :-)

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it's you

  • #5
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    If I substitute pw.lasso for myform, will it still work?

    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?
    Thanks!

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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() );'

  • #7
    New Coder
    Join Date
    Feb 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Another possibility is:

    Code:
    <script type="text/javascript">
    
         onload=
         function getFocus()
         {
              var toFocus = document.getElementById("id_of_element");
              toFocus.focus();
         }
    
    </script>
    Cheers
    Asp.Net 1.1 - 20 hrs
    CSS 2.0 - 3 hrs
    XHTML 1.1 - 5hrs
    JavaScript - 5hrs
    Cross-Browser Compatibility - Priceless

  • #8
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ... 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!))

  • #9
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First post ... found this post on google. GREAT source, thanks for the code pyth


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •