views:

210

answers:

4

In the following function it goes through the if and the else, why is that?

function test(){
        $(".notEmpty").each(function() {
         if($(this).val() === ""){
            alert("Empty Fields!!");
            return;
           }
         else{
                AddRow_OnButtonClick('tblMedicationDetail',6);
              }
     });

}

Is there any if and else statement on jquery that I am not aware of?

AddRow_OnButtonClick is a function to add a new row with 6 textboxes which I am attaching a className 'notEmpty' if any of the text boxes are empty the function AddRow_OnButtonClick should not be called and an alert should pop up.

I dont know what am I doing wrong.

Thanks

+1  A: 

I've structured your code to make it a little more readable, but I'm not sure why both the if and the else are both being called:

function test(){
    $(".notEmpty").each(function() {
        if($(this).val() === ""){
            alert("Empty Fields!!");
            return;
        } else {
            AddRow_OnButtonClick("tblMedicationDetail",6);
        }
    });
}
Neurofluxation
+3  A: 

The return statement only returns from the function supplied to .each. I suspect the problem you're having is that you want to return from test(). As it is, after you return from the inner function you'll still apply the inner function to the rest of the elements.

If I'm right, and you want to break out of the .each() altogether, put return false instead (see jQuery docs).

Skilldrick
+1  A: 

Combining the existing two answers leads me to believe this will work:

var emptyFields = false;

function test(){
    emptyFields = false;
    $(".notEmpty").each(function() {
        if($.trim($(this).val()) == "") {
            emptyFields = true;
            return false; // break out of the each-loop
        } 
    });
    if (emptyFields) {
        alert("Empty Fields!!");
    } else {
        AddRow_OnButtonClick("tblMedicationDetail",6);
    }
}

The basic idea is that you need to use your .each call to only determine if a field is empty. After this, you can deal with the information using the if else statement.

Codesleuth
See my edit - you can just do `return false` and you don't need to maintain a status variable.
Skilldrick
@Skilldrick: Thanks for the tip, I'm not much a of jQuery developer yet so it's good to know these things.
Codesleuth
That's it!!!, but there is still a problem, if the user types a space, it will consider as not empty, space should not be consider as not empty, but empty field. Thanks a lot.
Cesar Lopez
I considered this approach, but I remembered reading something about returning from the `.each` early. It is a bit confusing though - `return` acts like `continue` would in a loop, and `return false` acts like `return` usually would. brainache...
Skilldrick
Added the `$.trim()` method around the `.val()` line to ensure it classes whitespace as an empty field. See http://api.jquery.com/jQuery.trim/
Codesleuth
@Skilldrick: when `return false` is used, how do I read the result? It looks to me that `.each` returns a `jQuery` object, but I've no idea how the result is stored within this
Codesleuth
A: 
if($(this).val() == ""){

2 equals signs not 3 - That MIGHT be your problem...?

Stefan
Thanks, I tried but it was not that.
Cesar Lopez
Worth a shot, I see the answer above now :)
Stefan