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

231
Visualizações
Autonumbering upon keyboard press

I have an autonumbering when Ctrl and Enter are pressed. My current problem is, when i start my autonumbering at the 3rd textbox, my 2nd textbox is getting affected. How can i retain the other values of my textbox? The textbox before them should not change their value.

Thank you.

$(document).on('keyup', '.leg', function(e) {
  let value = parseInt(e.target.value);
  if ((e.ctrlKey || e.metaKey) && (e.keyCode == 17 || e.keyCode == 13)) {
    //loop through all values...
    $(".leg").each(function(i) {
      if (i !== 0) {
        $(this).val(++value); //assign new value..
        $(this).trigger("change") //call your change event to handle further...
      }
    })
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" class="leg">
<input type="text" class="leg">
<input type="text" class="leg">
<input type="text" class="leg">

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can create a variable to keep track of whether or not the each loop has crossed the target element and then only modify the input's value when it is true. This will make sure you only modify the inputs after the target.

See runChange in example:

$(document).on('keyup', '.leg', function(e) {
  let value = parseInt(e.target.value);
  if ((e.ctrlKey || e.metaKey) && (e.keyCode == 17 || e.keyCode == 13)) {
    //loop through all values...
    let runChange = false;
    $(".leg").each(function(i, el) {
      if (runChange) {
        $(this).val(++value); //assign new value..
        $(this).trigger("change") //call your change event to handle further...
      }
      if (e.target == el) runChange = true;
    })
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" class="leg">
<input type="text" class="leg">
<input type="text" class="leg">
<input type="text" class="leg">

about 4 years ago · Santiago Trujillo Relatório

0

Try to drop checking of 0'th input and change all inputs every time:

$(document).on('keyup', '.leg', function(e) {
  let value = parseInt(e.target.value);
  const inputIndex = $(this).index() - 1;
  if ((e.ctrlKey || e.metaKey) && (e.keyCode == 17 || e.keyCode == 13)) {
    //loop through all values...
    $(".leg").each(function(i) {
      if (i !== inputIndex) {
        $(this).val(++value); //assign new value..
        $(this).trigger("change") //call your change event to handle further...
      }
    })
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" class="leg">
<input type="text" class="leg">
<input type="text" class="leg">
<input type="text" class="leg">

about 4 years ago · Santiago Trujillo 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