Skip to content Skip to sidebar Skip to footer

Javascript Switch Within For-loop Break; Conflicts

I have switch nested with loop in JavaScript like: for (var i = 0; i < checkBoxIds.length; i++) { if ($('#' + checkBoxIds[i]).prop('checked')) { var id = che

Solution 1:

That is exactly what labels are for:

theloop:
  for (var i = 0; i < checkBoxIds.length; i++) {
    ...

    switch (id.substring(id.length - 3)) {

      ...
        break theloop;
        // continue theloop;

Solution 2:

Instead of this

 continue; else break; //else return

Try this

continue; else break iWantHere; //Break with label

Add this label iWantHere where you want your control to go

Example

 for(...)
 {
    switch('a')
    {
       case 'a': break iWantHere; // This will exit out of loop
       default:
    }
 }  

 iWantHere :
  // Rest of your code

Post a Comment for "Javascript Switch Within For-loop Break; Conflicts"