1

I have mentioned my code below, i have a problem with return flase; inside a javascript each function

$(".current_patient_medicine").each(function (i) {
    var id = this.id;
    var value = $("#" + id).val();
    if (value.trim() == "") {

        check = false;
        alert("Medicine Name cannot left Blank.");
        return false;
    } else {
        check = true;
    }
});
$(".current_patient_medicine_days").each(function (i) {
    var id = this.id;
    var value = $("#" + id).val();
    if (value.trim() == "") {

        check = false;
        alert("Days Field cannot left Blank.");
        return false;
    } else {
        check = true;
    }
});

here the first condition alert "Medicine Name cannot left blank" showing well but after that the second alert also showing

8
  • Use break instead of return false if you don't want to make other loops. Commented Feb 17, 2015 at 7:04
  • Nitpick: your code's indentation makes it difficult to read. Have a look at a Javascript style guide Commented Feb 17, 2015 at 7:05
  • break must be inside a loop or switch, so it's throwing javascrip error Commented Feb 17, 2015 at 7:06
  • 1
    @panther: you can't use a break statent here because this isn't a Javascript loop construct like for or while. .each in jQuery is a higher-order function; break is an illegal statement in this context: cl.ly/image/0M3C1o0P280T/… Commented Feb 17, 2015 at 7:08
  • works fine - jsfiddle.net/arunpjohny/gnf7ath2/1 Commented Feb 17, 2015 at 7:14

3 Answers 3

2

You have the return false in an inner function call, it wouldn't stop the execution flow of the external function so you need

var check = true;
$(".current_patient_medicine").each(function (i) {
    var value = this.value;
    if (value.trim() == "") {
        check = false;
        alert("Medicine Name cannot left Blank.");
        return false;
    }
});
if (!check) {
    return false;
}
$(".current_patient_medicine_days").each(function (i) {
    var value = this.value;
    if (value.trim() == "") {
        check = false;
        alert("Days Field cannot left Blank.");
        return false;
    }
});
if (!check) {
    return false;
}

$('button').click(function() {
  var check = true;
  $(".current_patient_medicine").each(function(i) {
    var value = this.value;
    if (value.trim() == "") {
      check = false;
      alert("Medicine Name cannot left Blank.");
      return false;
    }
  });
  if (!check) {
    return false;
  }
  $(".current_patient_medicine_days").each(function(i) {
    var value = this.value;
    if (value.trim() == "") {
      check = false;
      alert("Days Field cannot left Blank.");
      return false;
    }
  });
  if (!check) {
    return false;
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="m1" class="current_patient_medicine" />
<input id="d1" class="current_patient_medicine_days" />
<br />
<input id="m2" class="current_patient_medicine" />
<input id="d2" class="current_patient_medicine_days" />
<br />
<button>Test</button>

Sign up to request clarification or add additional context in comments.

Comments

2

Rule of thumb: avoid return statements inside $.each statements

Rethink the solution:

function logic(someArray)
{
    var result = false;
    $.each(someArray, function(index,item){

        if(item == someValue)
        {
           result = true;
        }

    });

    return result;
}

This problem costed me a complete day to discover and fix. OMG!

Hope This helps someone.

1 Comment

so many answers telling where to put the correct return false statement. But nothing worked since the problem was the logic and NOT the implementation. This saved my day. Thank you.
0

Check out the documentation for $.each():

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

I've slightly simplified your codez:

var check = true;

$(".current_patient_medicine").each(function() {
  // The empty string ("") is falsey, so we can remove the comparison.
  // Also, no need to double-select the current element,
  // instead get the val() directly:
  if (! $(this).val().trim()) {
    alert("Medicine Name cannot left Blank.");
    check = false;
    return false;
  }
});

$(".current_patient_medicine_days").each(function() {
  if (! $(this).val().trim()) {
    alert("Days Field cannot left Blank.");
    check = false;
    return false;
  }
});

This code will alert the first time it sees an empty .current_patient_medicine value, and then it will alert the first time it sees an empty .current_patient_medicine_days value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.