Slasher
Slasher

Reputation: 618

if statement (jQuery)

I was unable to figure out, what was causing 10 that keeps to be displayed red. Other numbers are displaying like it should, but only on number 10 if statement fails to compare proper value.

$( document ).ready(function() {
$("thead tr td:nth-child(3)").each(function(){

    var compare = $(this).text()

        if( compare == 'null' ) {
             $(this).text('No rating found').parent('tr').addClass("table-danger");
        }
          else if ( compare <= '5' ) { //Number 10 keeps comparing whit this statement       
                        $(this).parent('tr').addClass("table-danger");    
          }
          else if ( compare > '5' &&  compare < '7' ) {       
                        $(this).parent('tr').addClass("table-info");    
          }
           else if ( compare >= '7' ) {       
                        $(this).parent('tr').addClass("table-success");    
          }
     });
});

Something is wrong whit first else if statement. And number 10 should be green ("table-success")

Live Demo: https://jsfiddle.net/gah1m33d/1/

Upvotes: 0

Views: 64

Answers (1)

ruithebear
ruithebear

Reputation: 71

The problem is that you are comparing 10 [a number] with a string value of 5. Try:

else if ( parseInt(compare) <= 5 )

Upvotes: 3

Related Questions