Reputation: 163
I can not stop the for loop so that there was only the first input text that is colored red if it is empty and it turns green if filled. I try with if in the for loop but I'm missing something. http://jsfiddle.net/4dvw6/1/
$(document).on('click', '#Check', Check);
function Check() {
var length = $('#PA .argument').length;
for (i = 1; i < length + 1; i++) {
var val = $("#PA tr:eq(" + i + ")").find(".argument").val();
if (val === "") {
$("#PA tr:eq(" + i + ")").find(".argument").css('background', 'red')
} else if (val !== "") {
$("#PA tr:eq(" + i + ")").find(".argument").css('background', 'green')
}
}
}
Upvotes: 0
Views: 693
Reputation: 1453
$(document).on('click', '#Check', Check);
function Check() {
var length = $('#PA .argument').length;
for (i = 1; i < length + 1; i++) {
var val = $("#PA tr:eq(" + i + ")").find(".argument").val();
if (val === "") {
$("#PA tr:eq(" + i + ")").find(".argument").css('background', 'red')
break;//return;
}else {
$("#PA tr:eq(" + i + ")").find(".argument").css('background', 'green')
}
}
}
Upvotes: 1
Reputation: 57105
Use break
if (val === "") {
$("#PA tr:eq(" + i + ")").find(".argument").css('background', 'red');
break;//break for loop here
}
Upvotes: 3