SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is it possible to set the FOCUS() dynamically with javascript

    hi,
    This below code is working for IE but not working for FIREFOX....


    Code:
    <html>
    <head>
    <script language="JavaScript">
     
     function checkme(strVal){
     var id=strVal;
                alert(id);
         //document.all[strVal].focus();---->(IE) FOR IE it is working but not working for mozilla firefox
         document.forms[strVal].focus(); //(FIREFOX)
     }
    </script>
    </head>
    <body>
    <form>
    NAME..<input type="text" name="empName" size="5" maxlength="4" onChange="checkme(this.value)"/><br>
    ID....<input type="text" name="id" value=""/><br>
    DEPT..<input type="text" name="dept" value=""/><br>
    </form>
    </body>
    </html>


    thx & rgds,

    Ajay.
    Last edited by AJAY KUMAR; Jul 5, 2007 at 21:40.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,833
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    document.forms[0].strVal.focus();

    should work on all browsers.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi
    document.all[strVal].focus();----> is wroking for IE, NOT IN FF
    ocument.forms[0].strVal.focus();------> is not supported by FF.

    Can u tel me some other way to focus dynamically.

    Thx & rgds,
    Ajay.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,833
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You must have made a typo then since the old way of setting it in all browsers is:

    document.forms[0].strVal.focus();

    unless you also give the form a name in which case you can use:

    document.formname.strVal.focus();


    there is never a need to use document.all unless you are trying to get something to work in IE4.

    The other alternative is to give the field an id and then you can use

    document.getElementById('strVal').focus();

    (assuming you use id="strVal"0
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •