peterHasemann
peterHasemann

Reputation: 1590

Focus the first element on a new row

When I add a new row to a Html table, I want to focus the first Cell of this row.

My current code is

$('table td').first().focus();         // focus on the first cell of the row

but this selects the first cell of the entire table. What is needed to get the first cell of the added row?

Upvotes: 0

Views: 1951

Answers (2)

Eugene Tsakh
Eugene Tsakh

Reputation: 2879

If you add this row at the end of table you need to replace .first with .last in your code. But the best way is to do this when you add the row. For example:

$row = $('<tr>...</tr>');
$row.appendTo($container).find('td:first').focus();

Upvotes: 1

RobertAKARobin
RobertAKARobin

Reputation: 4274

That's saying, "Find the first td in table."

You want, "Find the first td in the last tr in table."

$('table tr').last().find('td').first().focus();

That's assuming the last tr is the row that was just added.

Upvotes: 1

Related Questions