Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this break statement valid in jquery/javascript?

I have a function which selects a text based on the input string. If both matches i make it selected. PFb the function,

function setDropdownTextContains(dropdownId,selectedValue,hfId){
            $('#'+dropdownId+' option').each(function(){

                     if($(this).text() === selectedValue){
                         $(this).attr("selected", "selected");
                         break;
                     }
            });
                 $('#'+hfId).val("ModelName doesnt match");
        }

I get the below error unlabeled break must be inside loop or switch ... What am i doing wrong??

like image 874
ACP Avatar asked May 06 '13 10:05

ACP


Video Answer


2 Answers

The exception text is quite descriptive. You really can't use break statement inside if clause. In your case you should use return false to stop the .each() iteration.

like image 120
VisioN Avatar answered Oct 12 '22 00:10

VisioN


A break statement is designed to end a for, while or do-while loop or a switch statement. It has no side effects where you are using it. What are you trying to achieve?

In your specific case, just return false

like image 33
flavian Avatar answered Oct 12 '22 01:10

flavian