40
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

jQuery .each()のループはbreakじゃ抜けれない

Last updated at Posted at 2017-01-23

今更だけどjQueryの.each()は「break」じゃ抜けれないんですって

いつものノリで「break」って書いたらエラーが出た。
え?breakじゃないの???

break.js
$(".test").each(function () {
    if($(this).val === 1){
        alert("1ですね");
    }else{
        return break;
    }
}); 

![alt](https://qiita-image-store.s3.amazonaws.com/0/147005/5e9e4976-f066-4ce4-181a-dbc146ed4640.png =*100)

jQueryの.each()は「return false」で抜けるらしい

これが正解らしい。

break.js
$(".test").each(function () {
    if($(this).val === 1){
        alert("1ですね");
    }else{
        return false;
    }
}); 

因みにcontinueしたい場合は「return true」

continue.js
$(".test").each(function () {
    if($(this).val === 1){
        alert("1ですね");
    }else{
        return true;
    }
}); 

今まで.each()をブレイクしたことなかったのかなー俺

40
27
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?