Hello-World
Hello-World

Reputation: 9555

check for css background color in any child in `<tr>`?

How do I check for if any child of a <tr> contains a background:blue; then do something?

$('table tr').each(function(index,value){
    if (value.has('background:blue')){
        alert (index); //do something to this row
    }
});
<table>
    <tr>
        <td>dfgdfgdfgdf</td>
        <td><input style="background:blue; value="test" /></td>
        <td>gdfg</td>
        <td><p style="background:blue;>cvcxvcxvx</p></td>
    </tr>
    <tr>
    <td>gfdgdfg</td>
        <td>gdfgfd</td>
        <td style="background:blue;>dfgdf</td>
        <td><div><div><button style="background:blue; type="submit"/></div></div></td>
    </tr>
    <tr>
    <td>gfdgdfg</td>
        <td>gdfgfd</td>
        <td>dfgdf</td>
        <td>dfgdf</td>
    </tr>
    <tr>
    <td>gfdgdfg</td>
        <td>gdfgfd</td>
        <td>dfgdf</td>
        <td><div><div><button style="background:blue; type="submit"/></div></div></td>
    </tr>
</table>

Upvotes: 1

Views: 169

Answers (2)

fdehanne
fdehanne

Reputation: 1718

With that ?

$(function() {
    $('tr').find('*').each(function(i) {
         if ( $(this).css('background-color') == 'rgb(0, 0, 255)' )
            alert($(this).text());
    });
});​

http://jsfiddle.net/xHgVf/3/

Upvotes: 2

Rory McCrossan
Rory McCrossan

Reputation: 337646

This won't be quick, but it will work:

var $blueElements = $("tr *").filter(function() {
    return $(this).css("background-color") == "blue";
});

Is there a reason you cannot use a CSS class to put the blue background on each element? Then you could just use that class to select them: $(".blue-element")

Upvotes: 2

Related Questions