SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Focus function not working as intended

    Why does this bit of script not focus the cursor in the specified field in the specified form?

    Code:
       function focusMe(formName, field) {
         document.formName.field.focus();
       }
    I call this code like this:
    HTML Code:
       <body onload="focusMe('myform', 'mycheckbox')">
    Any pointers?
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  2. #2
    Non-Member QiSoftware's Avatar
    Join Date
    Oct 2004
    Location
    U.S.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi --

    I can't see your code but here is what should work:
    document.formname.formelementname.focus()

    Make sure it is a form element that can receive focus.

    Q...

  3. #3
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I'll give it a go!
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is here
    Quote Originally Posted by garlinto
    function focusMe(formName, field) {
    document.formName.field.focus();
    }
    Unlike php and perl, javascript doesn't perform identifier resolution when you refer to object's field using dot notation. Example:
    Code:
    var obj = { foo : "bar" }
    alert(obj.foo) // bar
    var baz = "foo"
    alert(obj.baz) // undefined
    You should use bracket notation to force resolution:
    Code:
    alert(obj[baz]) // bar
    and in your snippet this would look like
    Code:
    function focusMe(formName, field) {
        document.forms[formName][field].focus();
    }
    hth

  5. #5
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    stereofrog, thanks for the explinations and code. I got my script working, and I thank you.
    However, in your explination about using brackets to force resolution, I had most of the example code working except for this:
    Code:
       var baz = "foo";
       alert(obj[baz]); // foo
    I ran your code examples, since that really helps me understand the points fellow coders are making. Did I just not do something right?
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  6. #6
    Non-Member QiSoftware's Avatar
    Join Date
    Oct 2004
    Location
    U.S.
    Posts
    366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have firefox and can see your code now... sorry...

    That doesn't work because <body=onload...> occurs before the form is initialized....
    Why not just make it a freestanding setting... without calling a function... It maybe the way the argument is being passed too...

    Q...
    Last edited by QiSoftware; Dec 31, 2004 at 15:59.


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
  •