Results 1 to 3 of 3

Thread: how to set focus from variable ?

  1. #1
    Join Date
    Apr 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy how to set focus from variable ?

    hi all,

    In a page code like this
    var f;
    f =document.Form1.output1.value;
    document.Form1.f.focus();

    in this,the variable 'f' has the value of the 'output1' box
    how do i take this as ID for focus text box.i tried the above.
    the above code didn't work .
    how to do this
    help me plz......

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Code:
    document.Form1.elements[f].focus();
    or

    Code:
    document.getElementById(f).focus();

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by codeexploiter View Post
    Code:
    document.Form1.elements[f].focus();
    or

    Code:
    document.getElementById(f).focus();
    The former is preferable, though I would write it:

    Code:
    function setFocus(controlName) {
        var element = document.forms.Form1.elements[controlName];
    
        if (element.focus) element.focus();
    }
    which would be called using:

    Code:
    setFocus(f);
    There are browsers (at least some versions of Safari, as I recall) that don't implement the focus method.
    Mike

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
  •