1

Going off this question: Selecting elements with a certain background color

I wanted to change the background of ALL elements with background of a certain color. I tried the method mentioned in the answer, but it doesn't seem to work for me.


jQuery:

// using string
$('*').filter(function(){
    return ( $(this).css('background-color') == 'green' );
}).css('background', 'blue');

// using hex
$('*').filter(
    return ( $(this).css('background') == '#a3a3a3' );
}).css('background', 'red');

Here's the JS Bin

Etep
  • 2,721
  • 4
  • 17
  • 28

1 Answers1

1

You should convert hex to RGB first and then compare

Example

<script>
    function hexToRgb(hex) {
        var bigint = parseInt(hex, 16);
        var r = (bigint >> 16) & 255;
        var g = (bigint >> 8) & 255;
        var b = bigint & 255;

        return "rgb(" + r + ", " + g + ", " + b + ")";
    }

    $('*').filter(function () {
        return ($(this).css('background-color') == hexToRgb('a3a3a3'));
    }).css('background', 'green');
</script>

jsFiddle

Surya Narayan
  • 558
  • 2
  • 16