Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to validate elements inside a javascript function which contains two jQuery callback loops. Based on the conditions I want to return true/false from the inner jQuery loop and that should be sent back to the calling method of javascript. If the result of the inner loop is true the loop should stop running.

if(validate(key)){
}
else{
}
function validate(key) {
    $jquery.each(function(){
        $jquery.each(function(){
            if(){
                return true;
            }
            else{
                return false}
        })
    })
}
share|improve this question
add comment

4 Answers

I think this is what you're looking for, this will stop both loops when the true condition is met

function validate(key) {
    var result = false;
    $jquery.each(function(){
        $jquery.each(function(){
            if(){
                result = true;
                return false;//break inner loop
            }
        });
        if(result)
            return false; //break outer loop if we got true in inner
    });
    return result;
}

Demo fiddle You can open your console and see that the loop stops when the true condition is met

share|improve this answer
add comment

The jQuery docs give a similar example. The docs state 'you can stop the loop from within the callback function by returning false.'

So it sounds like you need to reverse you're terminology and return false when you want the loop to stop.

Let's say we have a bunch of div elements with li elements nested inside. If we wanted to stop the inner loop when it reaches an li with specific content we could do this:

  $( "div").each(function ( index, domEle) {
      $( "li", domEle ).each(function ( index, domEle2) {
        var areWeDone;
        $( domEle2 ).css( "backgroundColor", "yellow" );
              if ( $( domEle2 ).is(":contains('Here')") ) {
                result = true;
                return false;
              } else {
                result = false;
                return true;
              }
      });

      if (result == true) {
          return false;
      } 

  });

Here's the full jsFiddle.

share|improve this answer
 
Your example works but it's not what the OP needs, you're reversing the semantics of the function by returning false when the condition is met –  koala_dev Aug 9 '13 at 17:42
add comment
function validate(key) {
    var result;
    $jquery.each(function() {
        $jquery.each(function() {
            if () {
                result = true;
            } else{
                result = false
            }
            return false;
        });
        if (typeof result !== 'undefined') {
            return false;
        }
    });
    return result;
}
share|improve this answer
 
But how can i stop iterations of both the jquery loops once i reach true, I tried using break based on result but did not work –  user2634485 Aug 9 '13 at 6:49
2  
The jQuery each function will stop its iterations if the called function returns false. –  Thierry J. Aug 9 '13 at 7:01
 
@ThierryJ. Didn't know that, always learn something new :) –  jcubic Aug 9 '13 at 8:30
 
Won't this return the last value of result, regardless of what happened on the other iterations of $.each()? –  Jeff B Aug 9 '13 at 15:58
 
@JeffB that's right, this answer does not accomplish what the OP needs –  koala_dev Aug 9 '13 at 17:31
show 1 more comment
    function validate(key) {
    var result;
    $jquery.each(function() {
        $jquery.each(function() {
            if () {
                result = true;

            } else{
                result = false;

            }
            return result;
        });
    });

}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.