peterHasemann peterHasemann - 1 month ago 6
jQuery Question

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?

Answer

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.

Comments