Unable to Set Focus on the First Row of the Data Table

I have a web page coded in JSF. The web page has one input field and one table.

I have rendered both input field and table (using h:dataTabe tag) without problem.

I have also highlighted the first row of that table when the web page is loaded. No problem.

And I have also figured out JavaScript functions to move the highlighter down or up one row at a time using keyboard down arrow key or up arrow key.

But, I cannot set the focus on the first row of that table when the page is loaded. It is evident that I always have to click on the first row inside the table before using the keyboard keys to move the highlighter down.

I used the JavaScript "focus()", it just does not work for me. I do not know what went wrong. And do I have alternatives such as using listener? How do I code to use a listener?

Please help.

var firstRow = 1;

var currentRow;

var highlightedRow;

window.onload = function()

{

var table = document.getElementById('countriesList:countryTable' );

var trs = table.getElementsByTagName('tr' );

highlightRow( trs[ firstRow ] );

trs[ firstRow ].focus();

currentRow = trs[ firstRow ].rowIndex;

}

Message was edited by:

jiapei_jen

[1379 byte] By [jiapei_jena] at [2007-11-27 9:49:40]
# 1

This strikes me as a JavaScript issue only.

I suspect that the JS event handler you have set up is only listening for keyboard events originating within the table. Try listening for any keyboard event on the entire page. If you need help on how to do this, you are asking the wrong person.

RaymondDeCampoa at 2007-7-13 0:18:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
> But, I cannot set the focus on the first row of that table when > the page is loaded. Call the focus function at the end of the page, right before the closing </body> tag.
BalusCa at 2007-7-13 0:18:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Thanks for your reply.I have tried to put the "focus" function everywhere including right before the <body> tag. The "onkeydown" attribute is specified in the <h:dataTable ...> tag. The 'first" keyboard action is simply not heard by the table.
jiapei_jena at 2007-7-13 0:18:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Did you try putting it in the onLoad attribute of the <body> tag?
RaymondDeCampoa at 2007-7-13 0:18:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Thanks for all your attention and inputs.

In addition to specify the attribute:

onkeydown="processKeys( event )"

for the <h:dataTable ....> tag, I have to put

document.onkeydown = function(event) { processKeys(event); };

in my JavaScript.

Now, things are working.

jiapei_jena at 2007-7-13 0:18:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Hmm, I think that I misinterpreted the "focus" in your words. I understood that you want to set focus on an element inside a datatable on load, not on key.
BalusCa at 2007-7-13 0:18:15 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...