Focus on text field in TD

Discussion in 'Javascript' started by VA, Nov 11, 2005.

  1. VA

    VA Guest

    t=document.getElementById('mytable')
    is a HTML table with some input fields in its cells

    Why doesnt

    t.getElementsByTagName('tr')[1].firstChild.focus;

    put the focus on that text field? It doesnt give any errors, the focus
    just doesnt change.

    What am I doing wrong? Thanks
     
    VA, Nov 11, 2005
    #1
    1. Advertising

  2. VA

    Randy Webb Guest

    VA said the following on 11/10/2005 10:09 PM:

    > t=document.getElementById('mytable')
    > is a HTML table with some input fields in its cells
    >
    > Why doesnt
    >
    > t.getElementsByTagName('tr')[1].firstChild.focus;
    > put the focus on that text field? It doesnt give any errors, the focus
    > just doesnt change.
    >
    > What am I doing wrong? Thanks


    Maybe you forgot the () on your focus call (thats #1 guess).
    Maybe you have extra whitespace in your code.

    Show the HTML and JS code.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Nov 11, 2005
    #2
    1. Advertising

  3. VA

    RobG Guest

    VA wrote:
    > t=document.getElementById('mytable')
    > is a HTML table with some input fields in its cells


    By 'input fields' do you mean input elements?

    >
    > Why doesnt
    >
    > t.getElementsByTagName('tr')[1].firstChild.focus;


    You need to show the related HTML. If you have:

    <table id="mytable">
    <tr>
    <td>...</td>
    </tr>
    <tr>
    <td><input ...></td>
    </tr>
    </table>

    In Geko browsers (and likely some others too) your reference will be to
    a text node that will seem to be immediately after the second <tr> tag.


    In IE, your reference will be to the TD element, not the input element.
    If the HTML for the second TR element is:

    <tr><td><input ...></td></tr>


    Then in Geko browsers your reference will also be to the TD element.


    >
    > put the focus on that text field? It doesnt give any errors, the focus
    > just doesnt change.


    The W3C DOM 2 specification does not define a focus method for Interface
    HTMLTableCellElement, so at least some browsers will not implement it.

    >
    > What am I doing wrong? Thanks


    For those browsers that do have a focus method for table cells:

    t.getElementsByTagName('tr')[1].firstChild.focus();


    Should do. If you want to reduce error messages in browsers that don't
    support focus for cells, then:

    var c = t.getElementsByTagName('tr')[1].firstChild;
    if ( c && c.focus ) c.focus();


    will do better.

    If you are after the text input (which does have a focus() method
    defined in the specification) that is inside the cell, you should change
    your reference to it and not the cell and feature test anyway:

    if (document.getElementsByTagName){
    var c = t.getElementsByTagName('tr')[1].firstChild;
    var x = (c && c.getElementsByTagName('input')[0]);
    if (x && x.focus) x.focus();
    }



    --
    Rob
     
    RobG, Nov 11, 2005
    #3
  4. VA

    VA Guest

    Thanks, Rob. I was missing the

    var x = (c && c.getElementsByTagName('input')[0]);

    part. The TD had some other stuff in it that I didnt care about, I
    wanted just the INPUT element in it.

    Thanks!
     
    VA, Nov 11, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWlrZSBNb29yZQ==?=

    Set Focus to a Text Box Field on a User Web Control

    =?Utf-8?B?TWlrZSBNb29yZQ==?=, Dec 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    411
    Steve C. Orr [MVP, MCSD]
    Dec 9, 2004
  2. Dwayne Epps

    bring focus to text field on submit

    Dwayne Epps, Jul 16, 2003, in forum: Javascript
    Replies:
    0
    Views:
    177
    Dwayne Epps
    Jul 16, 2003
  3. Sound
    Replies:
    2
    Views:
    461
    Randy Webb
    Sep 28, 2006
  4. Roger
    Replies:
    3
    Views:
    338
  5. jr
    Replies:
    3
    Views:
    433
Loading...

Share This Page