Supun Silva
Supun Silva

Reputation: 590

Jquery input value if and or not working

I have tried to get sum of visible input textboxes values ( if value not equal to F or A)

 $(document).ready(function () {        
    $("#testbtn").click(function(){
    var total_hrs = 0;   
    $("input:text").filter(":visible").each(function(){    
    if (this.value != "F" || this.value != "A"){
        total_hrs += parseInt(this.value);    
    }    
    }); 
    alert(total_hrs);
    });

});

Here this.value != "F" only working. If I put both with or(||) this.value !="A", it will give me NaN.

Here is the example jsfiddle.

Thank you,

Supun Silva

Upvotes: 2

Views: 86

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You need to use and condition

$(document).ready(function () {
    $("#testbtn").click(function () {
        var total_hrs = 0;
        $("input:text").filter(":visible").each(function () {
            if (this.value != "F" && this.value != "A") {
                total_hrs += parseInt(this.value) || 0;
            }
        });
        alert(total_hrs);
    });
});

Demo: Fiddle

Upvotes: 2

Related Questions