After this code, each stops itself, but the code after (for example, alert('abc'); ) is executed.
How to stop the whole function, with a negative condition inside each ?

 function A() { $('input').each(function() { if (!$(this).val()) return false; }); alert('abc'); } 

  • it would be easier if js had a normal goto :-D - Grundy
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

 $('button').click(function () { if (!Array.prototype.every.call($('input'), function (input) {return $(input).val()})) { return; } alert('abc'); })(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input> <button>Test</button> 

    Use an extra variable to do this.

     function A() { let error = false; $('input').each(() => { if (!$(this).val()) { error = true; return false; } }); if(error) return false; alert('success'); } 
    • This will complicate the function. Is there a way in php break? - Gleb
    • @Gleb, no, in this case, using the each method is impossible - ThisMan
    • one
      @Gleb change each to the usual for loop and that's all) - andreymal

    If you want shortly so. Works.

     function A() { $('input').each(function() { if (!$(this).val()) throw new Error("Выход из функции!"); }); alert('abc'); }