SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding focus to form elements

    This seems like it should be a lot simpler, but it isn't. I need to have the cursor appear in the first input box on various pages. When I use the onLoad event in the body tag it works, but anywhere else it doesn't.

    The problem is aside from my intro page, most of my other pages use a header and footer on the server side. The body tag is inside the header so I don't have access to it on the other pages. I tried adding a simple function at the bottom of the page, but it does not work.
    Code:
    <script>
    function setFocus(){
      frm.Name.focus();
    }
    setFocus();
    </script>
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know if it makes much difference, but here is another way:
    <SCRIPT LANGUAGE="JavaScript">
    document.form_name.field_name.focus()
    </SCRIPT>

  3. #3
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I've tried that too.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  4. #4
    SitePoint Wizard davidjmedlock's Avatar
    Join Date
    Dec 2002
    Location
    Nashville, TN USA
    Posts
    1,688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your header code, you may have to put some logic around it:

    pseudocode:
    Code:
    if on page with form
    	output: <body onload="setFocus()">
    else
    	output: <body>
    end if

  5. #5
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by westmich
    This seems like it should be a lot simpler, but it isn't. I need to have the cursor appear in the first input box on various pages. When I use the onLoad event in the body tag it works, but anywhere else it doesn't.

    The problem is aside from my intro page, most of my other pages use a header and footer on the server side. The body tag is inside the header so I don't have access to it on the other pages. I tried adding a simple function at the bottom of the page, but it does not work.
    Code:
    <script>
    function setFocus(){
    frm.Name.focus();
    }
    setFocus();
    </script>
    You should be able to effect all of your <BODY> tags by writing it into one file...I suggest you make your include files more efficient so little problems like this are easy to handle...and besides, you should be able to use your <BODY> tag for things like this.

  6. #6
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Are you putting it in external js? the onload seems to trigger when the js has loaded, rather than the document.
    try

    window.onload=function() {....................}
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  7. #7
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Markdidj
    Are you putting it in external js? the onload seems to trigger when the js has loaded, rather than the document.
    try

    window.onload=function() {....................}
    Ah, thank you for reminding me...window.onload...

    Okay westmich, I wrote exactly what you wanted...

    Code:
    <script language="javascript">
    function focusField()
    {
    window.document.form1.text1.focus();
    }
    window.onload=focusField;
    </script>
    That'll work, and no OnLoad required...I still think you should make your server side files more efficient...but this is a good temporary solution for now.

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function focusField() { window.document.form1.text1.focus(); } window.onload=focusField; 
    That's a unecessary waste of namespace. Assign the function directly to the event property instead
    Code:
    window.onload = function()
    {
    	document.form1.text1.focus();
    }
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  9. #9
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for the help.

    In an ideal world, I would have more effecient headers and a lot more of the logic and structure moved to the server-side, however, there are a couple factors. 1) This is a project that was originally developed a couple of years ago and we're going back through and doing a major face-lift. I would have prefered to rebuild it from scratch but it wasn't in the budget. 2) Out of the three of us here that do actual web work, I am the only programmer, i.e. the only one that works outside of a WYSIWYG editor. Moving to .NET is going to remove some of these headaches, though.
    Last edited by westmich; Apr 18, 2003 at 06:48.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  10. #10
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    Code:
    function focusField() { window.document.form1.text1.focus(); } window.onload=focusField; 
    That's a unecessary waste of namespace. Assign the function directly to the event property instead
    Code:
    window.onload = function()
    {
    document.form1.text1.focus();
    }
    Oh for god's sakes...at least it works, quit yer yappin'...sheesh.

  11. #11
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gregc
    Oh for god's sakes...at least it works, quit yer yappin'...sheesh.
    Nope. I'm on a namespace mission. I've written an article for Sitepoint on javascript namespace issues that's in the editing process at this moment.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  12. #12
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    Nope. I'm on a namespace mission. I've written an article for Sitepoint on javascript namespace issues that's in the editing process at this moment.
    I'm behind ya, Beetle! Optimality rules ok!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  13. #13
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    Nope. I'm on a namespace mission. I've written an article for Sitepoint on javascript namespace issues that's in the editing process at this moment.
    Whatever, computer geek. I've never had any "namespace issues" with JavaScript.

  14. #14
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by M@rco
    I'm behind ya, Beetle! Optimality rules ok!
    I second the motion!


Bookmarks

Posting Permissions

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