jcuenod
jcuenod

Reputation: 58405

Break out of an if statement

JSHint is telling me not to use a label on an if statement (it seems that labels are only for loops as far as jshint is concerned).

forminjection: if (options.addToForm !== false) {
  if (!(options.addToForm instanceof jQuery)) {
    options.addToForm = $(element).closest("form");
    if (options.addToForm.length === 0)
    {
      options.addToForm = false;
      break forminjection;
    }
  }
  $(element).each(function(index){
    //do stuff
  });
}

So I'm getting a warning on the first line above but also (naturally) on the break line.

Update: This code works as intended but is obviously not the right way to achieve the end result. How should it be refactored?

Upvotes: 2

Views: 392

Answers (1)

jcuenod
jcuenod

Reputation: 58405

Okay, I'm testing this cleaned up solution

if (options.addToForm instanceof jQuery || options.addToForm === true) {
  if (options.addToForm === true) {
    options.addToForm = $(element).closest("form");
  }
  if (options.addToForm.length === 0) {
    // No form found
    options.addToForm = false;
  }
}
if (options.addToForm !== false) {
  $(element).each(function(index) {
    // Do Stuff
  });
}

Upvotes: 1

Related Questions