SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Location
    Clare, Ireland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question focus() not working

    I have a form for inputting data to a database. One of the fields in the form takes a unique identifier which forms the primary key in the db table. I am using Ajax to check that the data entered into this field by the user doesn't already exist in the db - this is working perfectly!
    When the user entered-data matches a record in the database table, they are warned of this in an alert window, and the data they entered is deleted, and the focus then automatically goes to the next field. What I want to do is return the focus to the same field. I'm using the focus() method but it doesn't work for me. Code is as follows:

    Code:
    function checkppsnExists() {
        if (ajaxrequest.readyState == 4) {
            if (ajaxrequest.status == 200) {
                /* get the response from the server */
                var match = ajaxrequest.responseText;
                //alert ("Server has responded with: " + ajaxrequest.responseText);
                if (match != null) { 
                    alert("This ppsn is already in the database!\nThe participant has already been added!");
                    // and delete ppsn from textbox
                    document.getElementById("ppsn").value = "";
                    document.getElementById("ppsn").focus();
                }
            } else {
                alert("Error! Request status is " + ajaxrequest.status);
            }
        }
    }
    I'd appreciate any help on this as it has me stumped.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you try with long element lookup from document form
    e.g document.formname.inputname.focus() instead of getting the reference from getElementById

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are repeating code, assign the HTML node ppsn to a variable and use the variable instead. getElementById returns null if element is not find, so try alerting that.

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    meaning that assign
    Code:
    ppn = document.getElementById("ppsn")
    
    // then
    ppn.value = ""
    ppn.focus();
    also check again if reference is null, so

    Code:
    ppn = document.getElementById("ppsn")
    
    
    // then
    if (ppn != null) {
    ppn.value = ""
    ppn.focus();
    }

  5. #5
    SitePoint Member
    Join Date
    Apr 2007
    Location
    Clare, Ireland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    You are repeating code, assign the HTML node ppsn to a variable and use the variable instead. getElementById returns null if element is not find, so try alerting that.
    I've tried that as well, Pepejeria. It still didn't give the focus back to the field. However I'll keep this change as it's cleaner code.

  6. #6
    SitePoint Member
    Join Date
    Apr 2007
    Location
    Clare, Ireland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by SimplyFu View Post
    meaning that assign
    Code:
    ppn = document.getElementById("ppsn")
    
    // then
    ppn.value = ""
    ppn.focus();
    This didn't make a difference, but it is better practice so I'm keeping it.

    Quote Originally Posted by SimplyFu View Post
    also check again if reference is null, so

    Code:
    ppn = document.getElementById("ppsn")
    
    
    // then
    if (ppn != null) {
    ppn.value = ""
    ppn.focus();
    }
    Checking if reference is null does the trick, SimplyFu. Thanks a million! It works.

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    setTimeout('document.getElementById("ppsn").focus()',0);
    This seems silly, but it works. Maybe because the timeout puts it in the window scope, where the alert button wants to return focus after a click.

  8. #8
    SitePoint Member
    Join Date
    Apr 2007
    Location
    Clare, Ireland
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    setTimeout('document.getElementById("ppsn").focus()',0);
    This seems silly, but it works. Maybe because the timeout puts it in the window scope, where the alert button wants to return focus after a click.
    It works alright. I think you're correct in pointing to the alert window as being the culprit.


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
  •