Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Find Elements By Background-Color

Tags:

jquery

Trying to access the Selected row of a GridView by using JQuery to find the row with the background-color attribute set to the SelectedRowStyle background color. That color is #FF6600. I've tried

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

But that just sets all the rows to orange.

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

That returns empty

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

Returns undefined

like image 437
jmease Avatar asked May 31 '12 21:05

jmease


People also ask

How to get the element by background color in jQuery?

click(function() { var color = $( this ). css( "background-color" ); $( "p" ). html( "That div is " + color + "." ); });

How to set the background color of the elements using jQuery?

Set the background color red of the following elements using jQuery. .add (elements) : Create a new jQuery object with elements added to the set of matched elements. Here .add () method adds more elements like "span", "textarea" etc. .css (propertyName) : Get the computed style properties for the first element in the set of matched elements.

What does find (selector) return in jQuery?

.find (selector) Returns: jQuery Description: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. version added: 1.0.find (selector)

How do I change the color of a Div using CSS?

$( "div" ).click(function() { var color = $( this ).css( "background-color" ); $( "p" ).html( "That div is " + color + "." ); }); Contribute your code and comments through Disqus. Next: Add the following class "myclass" to the matched paragraph elements.

What is find () method in jQuery?

A string containing a selector expression to match elements against. An element or a jQuery object to match elements against. Given a jQuery object that represents a set of DOM elements, the .find () method allows us to search through the descendants of these elements in the DOM tree and construct a new jQuery object from the matching elements.


1 Answers

Try the .filter method.

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

I haven't tested it, the rgb part may need to be adjusted to account for spacing.

Edit:

or better yet, this takes into account uppercase vs lowercase

var rows = $('tr').filter(function(){
    var color = $(this).css("background-color").toLowerCase();
    return color === "#ff6600" || color === "rgb(255, 102, 0)" ;
});
like image 150
Kevin B Avatar answered Sep 20 '22 14:09

Kevin B