Tony
Tony

Reputation: 12705

jQuery - find element basing on the css attribute

I have that div

<div style='width:20px; height:20px; background-color:COLORNAME'></div>

which is created in the for loop.

Lets say I want to find the div which background color is #0000FF. How to do it by jquery ?

Upvotes: 0

Views: 49

Answers (2)

eithed
eithed

Reputation: 4349

How about using:

$("[style*='background-color:#0000ff']");

Bear in mind, that it's based on string comparison, so spaces (and case) will matter.

A fiddle: http://jsfiddle.net/eithe/6mwD7/

Upvotes: 3

George
George

Reputation: 36794

The browser will always process the color and output it as an rgb value, so this should do:

$('div').filter(function(){
    return $(this).css('background-color') == 'rgb(0, 0, 255)';
});

JSFiddle

Upvotes: 1

Related Questions