Selecting nth-child in Jquery and conditional formatting not working

I have a table and i want to format certain data fields based on their content. So for example if a field is less than 95% it should be red.

This is the jquery i'm using

$(function(){
    $('#ConditionalTable td:nth-child(2n - 4)').each(function(){
        var Sales = $(this).text();
        if (Sales > '95%')  {
            $(this).css('backgroundColor', '#f76e6e'); 
        } else {
            $(this).css('backgroundColor', '#99faa0'); 
        }
    });
});

It currently selects every second column and turns it green.

However i want it to skip the first 3 columns (these contain titles) and only start applying the formatting from the 4th column.

So the 4th column would have formatting applied then the 6th, 8th etc.

Also for some reason no matter what value is in my table it's being turned green. So in my code i've said if Sales are greater than 95% it should be green, otherwise red. But this doesn't seem to be happening.

Answers


Change #ConditionalTable td:nth-child(2n-4) to #ConditionalTable td:nth-child(2n+4)

$(function(){
    $('#ConditionalTable td:nth-child(2n + 4)').each(function(){
        var Sales = parseInt($(this).text().replace('%',''),10);
        if (Sales > 95)  {
            $(this).css('backgroundColor', '#f76e6e'); 
        } else {
            $(this).css('backgroundColor', '#99faa0'); 
        }
    });
});

The value of n will be 0,1,2,3,..... in this case it will select 4,6,8,10,..... children

You can't compare the % values as in the code , so you need to convert it into integer or float and then compare


Need Your Help

wpf datagrid single click edit with DataGridTemplateColumn not working

c# wpf wpfdatagrid data-binding

I'm creating datagrid columns dynamically based on a parsed csv file. Essentially I'm following the technique here, however I'm using a DataGridTemplateColumn as I need to specify a combo as an ed...