SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    field focus problem

    Any ideas here would be appreciated. I'm stumped.

    I have a script that runs when the page is loaded to set focus to a field. If I have an alert immediately before I call focus() on the field everything is good. If the alert isn't there, the cursor never shows up in the field...however, it scrolls to the textbox and if I hit tab I move to the field immediately following it which would indicate I really do have focus on the field, yet the cursor isn't there so I can't start typing in the field. I thought it might be an issue with the page not being fully loaded but if that is the case why does it scroll down to the field and move to the next field after hitting tab. This is a hard one to explain and what complicates it more is that it's in an AJAX responseText environment so it's hard to nail down. Any ideas on this?

  2. #2
    SitePoint Enthusiast mrsmiley's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does the field that you set focus to have a readyonly attribute set on it? I guess more to the point does it behave like this in IE and Firefox or only one of them?

    Have you tried changing the order of the focus and alert calls? You actually may need to focus it twice (once before and once afterwards) although that would seem a little unconventional

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply. Actually the only reason I have the alert is to check to see if I have a reference to the object. So I got it to work by screwing around with the alert box, which shouldn't even be there in the first place. Without it, it doesn't work. The element is not disabled or read only.

  4. #4
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you show some code?

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Location
    USA
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a snippet of the code. I messed with it some more and noticed that if I call the putFocusOnField() function twice, it works, but if I only call it once, regardless of location, it doesn't work. I'm baffled.

    Code:
    <script>
    checkMedLink();
    
    resizeScrolling('div1', 'div2');
    
    
    putFocusOnField('<%=request.getParameter("focusField")%>');
    </script>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    
    
    </div>
    <script>
    
    putFocusOnField('<%=request.getParameter("focusField")%>');
    </script>
    </div>
    The putFocusOnField() function exists elsewhere looking like this:

    Code:
    function putFocusOnField(focusField){
    
        if(focusField != 'null'){        
            if(document.getElementById(focusField).type != 'hidden'){
                document.getElementById(focusField).focus();
            }
        }
    
    }

  6. #6
    SitePoint Addict Quaint's Avatar
    Join Date
    May 2004
    Location
    Netherlands
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The request object suggests to me that the page uses Ajax. Since the first A stands for Asynchronous, this might be the cause of your problems. The Ajax request first needs to complete it's transactions before it can do anything else. Meanwhile the page keeps loading.. The focus won't be set, because the request object isn't finished yet or something.. This would be a logical explanation, because you said that if you do something BEFORE the setFocus() (like alert, double request setFocus, etc) it does work.. The thing you do prior to calling the method delays the script, which gives the request more time.. I have actually experienced this before, could really be the cause of your problems (even though it sounds vague here...).

    Try something for starters:

    Use <body onLoad=""> to call the setFocus() method, and take it out of the request object!

    Quaint Tech
    - Blog on web development and web technology.


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
  •