You are viewing limited content. For full access, please sign in.

Question

Question

How do I set the focus to the first row of a table?

asked on August 11, 2022 Show version history

I'm using the following js to add rows to a table.  After the rows have been added, the focus jumps to the next field below the table. 

  $('.attendeesCount input').change(function(){
      // Delete existing rows
      $('.attendeesTable .cf-table-delete').each(function(){
        $(this).click();
      });

      // Count number of rows in table
      var n = $(this).val();

      // Add rows to table
      for(var i = 0; i < n; i++){
        $('.attendeesTable .cf-table-add-row').click();
        $('tr').last().find('td:first-of-type input').focus();
      };

I added

$('tr').last().find('td:first-of-type input').focus();

to try to force the focus but it's not working; I tried both inside and outside of the for loop.

Any suggestions?  Thanks!

 

0 0

Answer

SELECTED ANSWER
replied on August 11, 2022

$('.attendeesTable tr td[data-title="Attendee Name"] input').first().focus();

after the for loop

1 0

Replies

replied on August 11, 2022 Show version history

$('tr input').last().focus();

should work...

Oops sorry, you said first row

$('tr input').first().focus();

0 0
replied on August 11, 2022

Unfortunately, that does not work either.  Also, I checked in the console and there are no errors.

 

0 0
replied on August 11, 2022

What is the title of the column you want the focus to land on? 

0 0
replied on August 11, 2022

The column label is Attendee Name, the variable is Attendee_Name.

 

0 0
SELECTED ANSWER
replied on August 11, 2022

$('.attendeesTable tr td[data-title="Attendee Name"] input').first().focus();

after the for loop

1 0
replied on August 11, 2022

Excellent.  That works great.  Thanks.

0 0
replied on August 12, 2022

Sorry it took so long to blow the dust off of the neurons. :)

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.