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();