JQuery Find Elements by Background Color

Trying to access a selected GridView row using jQuery to find a row with the background-color attribute set to the background color of SelectedRowStyle. This color is # FF6600. I tried

var row = $("tr").find().css("background-color", "#FF6600");

But it just sets all the lines to orange.

var row = $("tr[background-color=#FF6600");

It returns empty

var row = $("tr").find().attr("background-color");

Returns undefined

+3
source share
3 answers

Try the method .filter.

var rows = $('tr').filter(function(){
    var color = $(this).css("background-color");
    return color === "#FF6600" || color === "rgb(255, 102, 0)" ;
});

I have not tested it, you may need to adjust the rgb part to account for distance.

Edit:

or better yet, it allows for uppercase and lowercase letters

var rows = $('tr').filter(function(){
    var color = $(this).css("background-color").toLowerCase();
    return color === "#ff6600" || color === "rgb(255, 102, 0)" ;
});
+7
source

background-colornot an attribute, this is a CSS property. You can try using .filter:

var row = $("tr").filter(function(){
    // Chrome returns "rgb(255, 102, 0)" instead of "#FF6600"
    return $(this).css('background-color') === "rgb(255, 102, 0)";
});
+6
source
$('tr').each(function(){
    if($(this).css('background-color') == '#ff6600'){
         //do your stuff
    }
});
0

Source: https://habr.com/ru/post/1664569/


All Articles