0

Is it possible to select elements based on the value of a CSS property. For example:

CSS rules:

div.blockWithBorder { border: 1px solid red; }

HTML markup:

<div id="A" class="blockWithRedBorder">...</div>
<div id="B" style="border: 5px dashed red">...</div>
<div id="C" style="border: 2px solid #FF0000">...</div>

I want to find all div elements with a red border. A, B and C match this query.

0

3 Answers 3

2

You could do this, but it would mean getting the computed style value for each element you were querying against which could be a very expensive operation.

Here's an example:

var a = [].slice.call(document.querySelectorAll("a"));

var redLinks = a.filter(function(i){
  window.getComputedStyle(i);
  var color = i.style.borderColor.toLowerCase();
  return (color === 'red' || color === '#f00' || color === '#ff0000') ? i : false;
});
Sign up to request clarification or add additional context in comments.

4 Comments

looks like the OP wants some kind of CSS selector which solves his problem, not any other methods (such as using jQuery, ...), if you say it's possible, you should post some CSS code, added with the note about the poor performance.
You could do this... yes but how?
Edited with an example that requires JavaScript. Of course he can't select for styles using CSS selectors.
Thanks. I simply forgot that selector can be expressed as a function!
1

You can filter set of elements:

var $divsBorderRed = $('div').filter(function(){
    return ~this.style.borderColor.indexOf("red") || ~this.style.borderColor.indexOf("rgb(255, 0, 0)") || ~this.style.borderColor.indexOf("#FF0000")
});

EDIT: or like this:

var $divsBorderRed = $('div.blockWithRedBorder, div[style*="#FF000"], div[style*="red"]');

As you can see, that's not great in all cases...

5 Comments

You are only comparing inline styles with this, you need to use the computed style to compare against styles that are set through the stylesheet.
@Jack that's correct! I don't know really why I'm answering this question because the obvious answer should be: "Don't do that!"
Concerning the computed style, why not just use: $(this).css("border-color")?
@nlips sure you can use it
@nlips that is just the jQuery flavor of the same thing. jQuery will call window.getComputedStyle on the element to get style value for 'border-color'.
0

Firstly, you SHOULD NOT have to code for this. A class like "blockWithRedBorder" or some selector should be present. Saying that incase you are looking for a quick fix below is something that should help you....

You could iterate through all the divs and read the border css property like this : jsfiddle

$("div").each(function(i, item){
    var selector = '#' + item.id;
    var yourValue = $(selector).css("border");
    // you can perform your 'red' color check here
    // make sure to match it with 'rgb(255, 0, 0)' and with color 'red'.
});

1 Comment

I think that $(item) will be more efficient than $('#'+item.id)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.