Return a value when using jQuery.each()?

48,972

Solution 1

You are jumping out, but from the inner loop, I would instead use a selector for your specific "no value" check, like this:

function validate(){
  if($('input[type=text][value=""]').length) return false;
}

Or, set the result as you go inside the loop, and return that result from the outer loop:

function validate() {
  var valid = true;
  $('input[type=text]').each(function(){
    if($(this).val() == "") //or a more complex check here
      return valid = false;
  });
  return valid;
}

Solution 2

You can do it like this:

function validate(){
    var rv = true;
    $('input[type=text]').each(function(){
        if($(this).val() == "") {
            rv = false;   // Set flag
            return false; // Stop iterating
        }
    });
    return rv;
}

That assumes you want to return true if you don't find it.

You may find that this is one of those sitautions where you don't want to use each at all:

function validate(){
    var inputs = $('input[type=text]');
    var index;
    while (index = inputs.length - 1; index >= 0; --index) {
        if (inputs[index].value == "") { // Or $(inputs[index]).val() == "" if you prefer
            return false;
        }
    }
    // (Presumably return something here, though you weren't in your example)
}
Share:
48,972
Prashant Lakhlani
Author by

Prashant Lakhlani

I am founder of Facile Technolab, a custom software develoment company in India. I have 13+ years experience with Microsoft, Node, Azure/AWS and DevOps related technology stacks. I love challenges and finding way through it. I am passionate about solution big business problems using technology. DevOps and AWS I have 3 years experience handling DevOps and AWS. I can implement CI/CD for your project using jenkins/Azure DevOps, AWS EC2, AWS ECR & ECS, Docker and related DevOps and AWS Stack. Technical Project Manager I have 5+ years experience managing projects, leading technical teams, product development using agile methodology with excellent skills on agile project management, team building, team motivation, goal setting, delivering softwares. .Net and .Net Core Expert I have 10+ years experience working with microsoft technology stack. Depending on business requirements, I played back end developer, full stack developer or lead developer roles in many .net stack projects. SharePoint Developer 8 years overall experience working with all versions of SharePoint. I can handle anything SharePoint. DotNetNuke Expert I am an expert dotnetnuke developer with more than 6 years of experience building large scale dnn based applications. I write about my leanings and experiences related to dotnetnuke and asp.net at TrickyCoder. I worked on dotnetnuke module development, dotnetnuke skinning, dotnetnuke migrations, dotnetnuke upgrades, catalook customization, dotnetnuke SEO, portal generation, scheduler development, news letters and almost all kind of development, customization and troubleshooting of DotNetNuke.

Updated on January 07, 2020

Comments

  • Prashant Lakhlani
    Prashant Lakhlani over 4 years

    I want to return false and return from function if I find first blank textbox

    function validate(){
     $('input[type=text]').each(function(){
       if($(this).val() == "")
         return false;
    });
    }
    

    and above code is not working for me :( can anybody help?

  • YahyaE
    YahyaE over 8 years
    @Nick Craver, It is a very old post but I have a question. Is there any risk above that function return true before each loop completed?
  • Admin
    Admin about 8 years
    Absolutely zero such risk because until .each() completes, it will not proceed to the last statement. @YahyaE
  • mae
    mae over 4 years
    The first example does not work when you need to trim the value first. The second example works, but it relies on a state variable, which is generally fine, but in case you wanted to do pure functional programming it would not be considered valid.