Daniel Diaz Daniel Diaz - 3 months ago 9
jQuery Question

JQuery find between two text values

I am running this code into my jquery script, with this method i can detect only values that are higher or lower but not between them.

For example: the first statement says lower than 1.

How to change it and make between 0-1.

Second statement between 1-29...

Third statement 29-49 and so on...

The code works fine, just need that between, because the statement with higher value overwrite all the others... Any thoughts ?

$(document).ready(function(){
$('.table td').each(function(){
if($(this).text() <1) {
$(this).css('background-color','#fff');
}
else if($(this).text() <29) {
$(this).css('background-color','#9A271C');
}
else if($(this).text() <49) {
$(this).css('background-color','#BA650E');
}
});
});

Answer

you need to use && and while you use .text() I think you will need parseInt(); as well

$(document).ready(function(){
        $('.table td').each(function(){
            var ThisNumber = parseInt($(this).text());
            if( ThisNumber  < 1) {                           // less than 1
                $(this).css('background-color','#fff');
            }
            else if( ThisNumber  >= 1 && ThisNumber <29) {   //between 1 - 29
                $(this).css('background-color','#9A271C');
            }
            else if( ThisNumber >= 29 && ThisNumber <49) {   // between 29-49
                $(this).css('background-color','#BA650E');
            }
        });
    });