Question: I want to break out of `.each()` iterations and it doesn't allow me to. Here is my code. Thanks for the help guys! Appreciate it.

```
$('#btn-submit-add').click(function(){
    var answerField = 1;
    $('.addAnswerChoice').each(function(){
        var answerChoice = $(this).val();
        if (answerChoice == ""){
            $('#answerChoice-'+answerField+'-Error').show();
            $(this).focus();
            return false; // this doesn't work
        }
        answerField++;
    });
    alert('doing stuff after');
});

```

Answer: 
This solution is almost like `each` only but short-circuits when the first `true` value is returned. So, you don't have to explicitly break out of the iterations.

```
Array.prototype.slice.call(document.getElementsByClassName("addAnswerChoice")).some(function(item) {
 var answerChoice = item.value;
 if (answerChoice == ""){     
        $('#answerChoice-'+answerField+'-Error').show();
        item.focus();
        return true;
 }
 return false;
});

```
From [documentation](http://api.jquery.com/jQuery.each/):

> 
> We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
> 
> 
> 

---

Edit:

if @nnnnnn is right, the code should look like:

```
$('#btn-submit-add').click(function(){
    var validated = true;
    var answerField = 1;
    $('.addAnswerChoice').each(function(){
        var answerChoice = $(this).val();
        if (answerChoice == ""){
            $('#answerChoice-'+answerField+'-Error').show();
            $(this).focus();
            validated = false;
            return false;
        }
        answerField++;
    });
    if (!validated)
    {
        return false;
    }
    alert('doing stuff after');
});

```