VA said:
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();
}