By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,825 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

Focus on text field in TD

P: n/a
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

Nov 11 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/
Nov 11 '05 #2

P: n/a
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
Nov 11 '05 #3

P: n/a
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!

Nov 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.