0

I'm new with JQuery and I want to refactor my code to focus on next empty input after typing enter key. I have the following .html:

<tr>
  <td>...</td>
  <td>
    <input type="text" name="" value="" class="search-product">
  </td>
</tr>

<tr>
  <td>...</td>
  <td>
    <input type="text" name="" value="" class="search-product">
  </td>
</tr>

And my .js.erb:

let next_input = $(this).closest('tr').next('tr').find('input:text');
while(next_input.val()) { next_input = next_input.closest('tr').next('tr').find('input:text'); }
next_input.focus();

It works like a charm! But I think there is too much logic and I was wondering if it can be refactored to a more simplistic way like:

$(this).next($("input(:empty)")).focus();
  • Maybe this sollution could been helpful https://stackoverflow.com/questions/23251559/find-first-empty-input-field – user3624394 Jul 28 '18 at 16:27
  • Maybe this sollution could be helpful https://stackoverflow.com/questions/23251559/find-first-empty-input-field – user3624394 Jul 28 '18 at 16:29

0 Answers0