Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include multiple color styles with a conditional statement in JavaScript

I'm trying to optimize a script that targets multiple rgb color styles. It looks like this:

var divs = document.querySelectorAll('div[style^="color"]');
[].forEach.call(divs, function(div) {
    if(div.style.color.includes('rgb(215, 218, 220)')){
        div.style.color="rgb(23, 23, 24)";
    }
});

I'd like to do the same thing which is done for target style rgb(215, 218, 220), for 255, 69, 0 and 113, 147, 255. How would that be accomplished most simply and efficiently?

like image 790
Lichtung Avatar asked Apr 23 '26 03:04

Lichtung


1 Answers

This is likely to be inefficient as it reads and write dom according to a string match but you could use RegExp.prototype.test instead of String.prototype.includes:

    var divs = document.querySelectorAll('div[style^="color"]');
    var regexp = /rgb\(215, 218, 220\)|rgb\(255, 69, 0\)|rgb\(113, 147, 255\)/;
    [].forEach.call(divs, function(div) {
        if(regexp.test(div.style.color)){
            div.style.color="rgb(23, 23, 24)";
        }
    });

like image 171
remix23 Avatar answered Apr 25 '26 15:04

remix23



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!