SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How do I set the focus on a text box by default?

    ok, I need some info on setting focus on a text box.

    I have about 60 txt boxes on each page and I need to set a specific box with ID "focusID" to have focus when the page loads.

    here is my code

    -------------
    <head>
    <script language="javascript">
    function setfocus(focusID)
    {
    document.getElementById(focusID).focus();
    }
    </script>
    </head>
    <BODY onload="setfocus(ID)">
    -------------

    any ideas?

    thanks,
    Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone?
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  3. #3
    Phil fillup07's Avatar
    Join Date
    May 2002
    Location
    Jacksonville, FL
    Posts
    1,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I saw an example of this... I can't remember off the top of my head though.

    Try setting the Tab Order to 0 for the one you want active.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do I set the tab order?

    also, I checked out Google.com since their input txt box is default selected and they have the same code (as I have)...

    Thanks for the reply,
    Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a little more info. I am getting a runtime error

    _______

    Line: 15
    Error: 'Defect_8' is undefined
    _______

    defect_8 is the Id and name of a txt box
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I modified the code to include a "'" on each sid of "ID" in the call to setfocus.

    OLD
    -------------
    <head>
    <script language="javascript">
    function setfocus(focusID)
    {
    document.getElementById(focusID).focus();
    }
    </script>
    </head>
    <BODY onload="setfocus(ID)">
    -------------

    NEW
    -------------
    <head>
    <script language="javascript">
    function setfocus(focusID)
    {
    document.getElementById(focusID).focus();
    }
    </script>
    </head>
    <BODY onload="setfocus('ID')">
    -------------

    I am not getting this runtime error

    _______

    Line: 10
    Error: 'document.form1.focusID' is null or not an object
    _______

    Any ideas?

    Thanks, Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, that last one worked I just had to make Defect_8 all upper case
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  8. #8
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok what is the id of the element in question. If it is ID then you have to place the ID in quotes. This is a redundant name and while there is no rule against using it. Don't. It just makes things confusing.

    Use CSS it is easier and more efficient
    <style type="text/css">
    input
    {
    text-transform:uppercase;
    }
    </style>
    </head>

    <body>
    <input type="text" value="">
    Hope that helps.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  9. #9
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is using the W3C DOM essential?

    If it isn't (and there's not really any massive reason it should in this instance) then you can avoid all the fuss about using Tab indexing and uppercase text by simply putting...

    <script type="text/javascript">document.formname.focusID.focus()</script>

    ...anywhere after the focusID text box in your code.

    Google and Hotmail have been using this since the year dot.
    It's alot simpler, cleaner and is supported by older browsers too (unlike the W3C DOM solutions you seem to be working with)
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  10. #10
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How does that fix the uppercase question?...

    And also with javascript it is generally considered sloppy to be throwing <script> tags in the middle of HTML. I avoid it like the plague and in 3 years of coding js I have had to do it once.

    But the point is a good one...just try calling a function with the onload event handler. Still cross browser and backwards compatible

    <body onload="giveFocus()">

    in the head

    Code:
    function giveFocus()
    {
         document.formname.elementname.focus();
    }
    This way you have also created a reusable function
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.


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
  •