Muramasa
Muramasa

Reputation: 163

Jquery - Stop for if condition

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

Answers (2)

Siddique Mahsud
Siddique Mahsud

Reputation: 1453

http://jsfiddle.net/4dvw6/2/

$(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

Use break

if (val === "") {
    $("#PA tr:eq(" + i + ")").find(".argument").css('background', 'red');
    break;//break for loop here
}

Fiddle Demo

Upvotes: 3

Related Questions