Focus on text field in TD

V

VA

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
 
R

Randy Webb

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.
 
R

RobG

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();
}
 
V

VA

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top