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

119
Visualizações
While form fields empty jquery or js

I need to do a check on a page that will show a message when the first 3 form fields on a page are all empty. I can write the checks and show the message, but I am not certain what the correct way to handle the "while" part is in js/jquery. I could just put this in a loop that runs every 100ms but I am 100% sure that its absolutely wrong.

var empty = true;    
$('input[type="text"].slice(0,3)').each(function() {
   if ($(this).val() != "") {
      empty = false;
      return false;
   }
});

if (empty) {
  document.getElementById('interactive-warning').style.display = 'none';
};

What is the "correct" way to do this?


The working code:

$(document).ready(function(){
  $('.form-text').slice(0,3).on('input', function() {
    var empty = true;    
    $('.form-text').slice(0,3).each(function() {
      if ($(this).val() != "") {
        empty = false;
        return false;
      }
    });

    if (!empty) {
      document.getElementById('interactive-warning').style.display = 'none';
    } else {
      document.getElementById('interactive-warning').style.display = 'block';
    };
  }).trigger('input');
});

I needed to execute on ready because the code needed to be added before the form.

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

0

You can listen to the input event, which will let you know when the user changes those values, eg:

move your existing code into function checkIfEmpty() { ... and add:

$('input[type="text"].slice(0,3)').on("input", checkIfEmpty);

(note there's no () after checkIfEmpty on the event)

If your inputs are changed via code, the UI events aren't raised, so you'll need an alternative. The easiest way is: when your code changes the inputs, also call checkIfEmpty().

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