Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Focus on text field in TD

Reply
Thread Tools

Focus on text field in TD

 
 
VA
Guest
Posts: n/a
 
      11-11-2005
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

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      11-11-2005
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/
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      11-11-2005
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
 
Reply With Quote
 
VA
Guest
Posts: n/a
 
      11-11-2005
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
Problem with focus on text field and submit button mahesh557 Software 0 05-23-2008 05:14 AM
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
Set Focus to a Text Box Field on a User Web Control =?Utf-8?B?TWlrZSBNb29yZQ==?= ASP .Net 1 12-09-2004 11:41 PM



Advertisments