Stanton Stanton - 2 days ago 4
HTML Question

Really basic jQuery: how can I check to see if a table cell has a value?

Been Googling this for over an hour, and getting answers to much more complex questions but not this simple one, and my Dummies book is no help.

I want to change the background of a row if a particular cell has a value. So:

$('#MyTable tr').each (function(index) {

$(this).css('background-color', '#FCDCE4');

});


Works fine to change the bg color in every row, but how can I do this only in rows where column 30 has a value? I could probably give my target cell a css class and do it that way, but I'd really rather learn to use jQuery properly (so that I don't need to keep posting elementary questions).

Answer

Description

You can use jQuery .children() and .eq() to get the cell 30.

Note that .eq() is zero based, so you need to use 29 to get the 30th cell

Sample

$('#MyTable tr)').each (function(index) {
    var cell = $(this).children("td").eq(29)

    // your if condition

    $(this).css('background-color', '#FCDCE4');
});

More Information

Comments