3

I have set the background-color of a row via CSS

$("#gv tr:has(td)").each(function () {
                $(this).css({ "background-color": "green" });
})

After this , certain rows have been added

How can I loop through the rows and only again find the "green" rows?

$("#gv tr:has(td)").each(function () {
             ? (if row == green){
               }
})
Peter PitLock
  • 1,823
  • 7
  • 34
  • 71
  • 1
    FYI There's no need to do an each function to set them, just do: `$("#gv tr:has(td)").css({ "background-color": "green" });` – Derek Apr 17 '13 at 15:51

4 Answers4

4

Use css() to get value of background-color

$("#gv tr:has(td)").each(function () {
     if($(this).css("background-color") == "green")
     {
     }
});

You can also use the DOM object to get color

Live Demo

$("#gv tr:has(td)").each(function () {
     if(this.style.backgroundColor == "green")
     {
     }
});
Adil
  • 146,340
  • 25
  • 209
  • 204
3

What if you decide to change the background-color from green?

As an alternative solution, if your application allows it, I would recommend simply adding a class to each row element that you wish to select later. This would be much more maintainable and self documenting than identifying an element by a background color. e.g.

<td class="main-class green></td>

If you added a class to the appropriate elements, then you could easily select all of your green elements by the their selector.

George Johnston
  • 31,652
  • 27
  • 127
  • 172
0

Try:

if ($(this).css("background-color") === "green")
MMM
  • 7,221
  • 2
  • 24
  • 42
0

If($(this).css("background-color") == "green") { ... }