The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> set focus to element inside <td>, set focus to element inside <td>
sakhi
post Mar 30 2010, 06:35 AM
Post #1





Group: Members
Posts: 3
Joined: 30-March 10
Member No.: 11,525



I have a html table in my web page, and this table contains some input elements (like text box, btton etc) as cell data. I have implemeted a functionality to navigate through each cell by arrow keys. Now i want to set the focus to the cell data (button or textbox) with arrow key navigation.

html table code snippet -


<td id="testTd"><input id="testButton" type="button" value="test" onclick="alert('test clicked!!')"/></td>

While navigating through keyboard i get the id of button but when i try to set focus, it does not seem to be working

javascript code snippet -

var td = tabMatrix[rmove][cmove].firstChild;
alert(td.id); // here i get the id of button
var button = document.getElementById(td.id);
button.focus;

any help or pointers would be of great help.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
raphael75
post Mar 30 2010, 02:00 PM
Post #2


Novice
**

Group: Members
Posts: 28
Joined: 12-November 07
Member No.: 4,295



QUOTE(sakhi @ Mar 30 2010, 07:35 AM) *

I have a html table in my web page, and this table contains some input elements (like text box, btton etc) as cell data. I have implemeted a functionality to navigate through each cell by arrow keys. Now i want to set the focus to the cell data (button or textbox) with arrow key navigation.

html table code snippet -


<td id="testTd"><input id="testButton" type="button" value="test" onclick="alert('test clicked!!')"/></td>

While navigating through keyboard i get the id of button but when i try to set focus, it does not seem to be working

javascript code snippet -

var td = tabMatrix[rmove][cmove].firstChild;
alert(td.id); // here i get the id of button
var button = document.getElementById(td.id);
button.focus;

any help or pointers would be of great help.



Try button.focus();
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 30 2010, 05:05 PM
Post #3


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



QUOTE(sakhi @ Mar 30 2010, 01:35 PM) *

var td = tabMatrix[rmove][cmove].firstChild;

alert(td.id); // here i get the id of button
var button = document.getElementById(td.id);

The above just seems to give the variable "button" the same value as the cell ID. Anyway you shouldn't need the button ID. Try this instead:

CODE
var button = td.getElementByTagName('input')[0];


You can also use

CODE
var button = td.firstChild;

but only if there's no whitespace or code formatting before the INPUT element.

Also use focus(); like raphael75 wrote.

BTW the Opera browser has a similar keyboard navigation functionality.


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 12:50 PM