I would like to select every div that has a red background color for example. is this possible in jquery?
<div style="background-color:red"></div>
<div style="background-color:white"></div>
<div style="background-color:red"></div>
<div style="background-color:yellow"></div>
thank you
Why do it the hard way? A better solution is to use CSS classes to give your elements their styles, then you can simply use the class-based selector.
You could use the jQuery filter:
But, as tvanfosson has said, the correct way would be to assign a CSS class to each of your divs, then you could call the objects using jQuery like this (which is the preferred way of working with jQuery):
I think you need a custom filter for that if you don't want to rely on a certain
style=xxx
attribute on the tag:First of all, I never recommend setting in-line styles on page elements. Assign CSS classes and control your background colors in an external CSS document.
And in your CSS:
Then it makes it easy to select the red ones by using:
$('div.red')
This code also works for CSS that is not defined in the style attribute of the tag: