Reputation: 15836
I have a function to check elements of class .sitem
and if any of them has blank value , it should return , else he code should go on.
if($(".sitem").each(function(ss, element) {
if($(this).val()=="")
{
alertify.error("item name can't be left blank");
return true;
}
return false;
}))
{
return;
}
//go on with your code
the function works when the value is blank it alertify me , and returns false , the problem when the item has value , it doesnt go on with code , any hints ?
Upvotes: 0
Views: 123
Reputation: 15742
return true // continue loop
return false // breaks loop
So,
var is_valid = true;
$(".sitem").each(function(ss, element) {
if($(this).val()=="") {
alertify.error("item name can't be left blank");
is_valid = false;
return false;
}
});
if(!is_valid) {
return;
}
Upvotes: 1
Reputation: 388316
The problem is .each()
returns a jQuery object which will always be truthy(irrespective of the values returned from the each handler.... if you return false it just terminates the further iteration of the element set)
One solution is to use a valid state variable like
var valid = true;
$(".sitem").each(function (ss, element) {
if ($(this).val() == "") {
alertify.error("item name can't be left blank");
valid = false;
return true;
}
return false;
})
if (!valid) {
return;
}
Upvotes: 1