Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

93
Visualizações
tab key focus next empty input in a grid

What I'm trying to achieve is that when tab key is pressed, the cursor will be focused on the next empty input in a grid component. Input field that has value will be skipped. If the cursor currently is in input field 2, and input field 3 has value, when tab is pressed, the cursor will jump to input field 4. This is to speed up the form entry time.

Below is the html grid component that I have created

<div class="col-md-6" id="dcNonRetainValue">
<fieldset class="ES-border frame-height-collapsed">
    <legend class="ES-border">{{ 'Data Collection' }} </legend>
    <collect-paged-data data="ui.dataCollectionItems" currentPage="ui.dataCollectionItemsCurrentPage" pageSize="ui.dataCollectionPageSize">
    </collect-paged-data>
</fieldset>

Trying to focus next input element with js.

setTimeout(function () {
   $('#dcNonRetainValue *:input:empty').focus();
}, 50);

It does not seem to work correctly. Any feedback is appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use filter function to select all the empty inputs. Then use eq function to select first input and use focus function. You can do like below Example:

$(document).ready(function() {

  $('input').on( 'keydown', function( e ) {
    if( e.keyCode == 9 ) {
    
      const allEmptyInput = $('input').filter(function() {
        return $(this).val() === ""; 
      });
      
      // Return if there is no Empty Input      
      if (allEmptyInput.length === 0) return;
    
      e.preventDefault();

      const currentInput = $(e.target);

      const nextAllEmptyInput = currentInput.nextAll('input').filter(function() {
        return $(this).val() === ""; 
      });
      
      // Focus on first input if last is focus
      if (nextAllEmptyInput.length === 0) {
        allEmptyInput.eq(0).focus();
        return;
      }

      const firstEmptyInput = nextAllEmptyInput.eq(0);
      firstEmptyInput.focus();

     }
  });
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

<label>First Input</label><br/>
<input type="text"/>
<br/>
<label>Second Input</label><br/>
<input type="text"/>
<br/>
<label>Third Input</label><br/>
<input type="text"/>

about 4 years ago · Juan Pablo Isaza Relatório

0

Using id will only give you the first element with that id. Use class instead.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda