Bipin
Bipin

Reputation: 354

focus Using Jquery <tr> <td>

$(this).closest('tr').next() td:eq(7).focus();

What is the error in this? I am new to jQuery. How can I focus using tr, td and table index?

$('.gridfield').keypress(function(e) {
    console.log(this);
    if (e.which == 13) {    
        var row_index = $(this).parent().index();    
        $(this).closest('tr').next() td:eq(row_index).focus();     
        e.preventDefault();
    }
});

Upvotes: 2

Views: 7519

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

The syntax isn't quite right. You can use either this:

$(this).closest('tr').next().find('td').eq(row_index).focus();     

Or this:

$(this).closest('tr').next().find('td:eq(' + row_index + ')').focus();     

They are logically identical.

Each cell contains input field

In that case use either one of these:

$(this).closest('tr').next().find('td').eq(row_index).find('input').focus(); 
$(this).closest('tr').next().find('td:eq(' + row_index + ') input').focus(); 

Upvotes: 1

Related Questions