Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!