Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7996785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:47:32+00:00 2026-06-04T14:47:32+00:00

I’d like to: Find a style attribute for all elements in the page (for

  • 0

I’d like to:

  1. Find a style attribute for all elements in the page (for instance: all elements that have color:#333;)
  2. Change this attribute for all of them (for instance from color:#333 to color:#444).

Do you have any suggestion on doing so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:47:33+00:00Added an answer on June 4, 2026 at 2:47 pm

    My suggestion is avoid doing this if at all remotely possible. Instead, use a class to assign the color value, and then you can look up the elements using the class, rather than the color value.

    As far as I’m aware, there’s no selector (not even in CSS3) that you can use to query a specific style value, which means looping through all elements (or it looks like you can restrict it to all elements with a style attribute) and looking at the element.style.color property. Now, the thing is, even though you write color: #333; in your style attribute, different browsers will echo it back to you in different ways. It might be #333, it might be #333333, it might be rgb(51, 51, 51), it might even be rgba(51, 51, 51, 0).

    So on the whole, a very awkward exercise indeed.


    Since you’ve said this is for a Chrome extension, you probably don’t have to worry as much about multiple formats, although I’d throw in the ones that we’ve seen in the wild in case Chrome changes the format (perhaps to be consistent with some other browser, which has been known to happen).

    But for instance:

    (function() {
    
      // Get all elements that have a style attribute
      var elms = document.querySelectorAll("*[style]");
    
      // Loop through them
      Array.prototype.forEach.call(elms, function(elm) {
        // Get the color value
        var clr = elm.style.color || "";
    
        // Remove all whitespace, make it all lower case
        clr = clr.replace(/\s/g, "").toLowerCase();
    
        // Switch on the possible values we know of
        switch (clr) {
          case "#333":
          case "#333333":
          case "rgb(51,51,51)": // <=== This is the one Chrome seems to use
          case "rgba(51,51,51,0)":
            elm.style.color = "#444";
            break;
        }
      });
    })();
    

    Live example using red for clarity | source – Note that the example relies on ES5 features and querySelectorAll, but as this is Chrome, I know they’re there.

    Note that the above assumes inline style, because you talked about the style attribute. If you mean computed style, then there’s nothing for it but to loop through all elements on the page calling getComputedStyle. Other than that, the above applies.

    Final note: If you really meant a style attribute with precisely the value color: #333 and not the value color:#333 or color:#333333; or color: #333; font-weight: bold or any other string, your querySelectorAll could handle that: querySelectorAll('*[style="color: #333"]'). But it would be very fragile.


    From your comment below, it sounds like you’re having to go through every element. If so, I wouldn’t use querySelectorAll at all, I’d use recursive descent:

    function walk(elm) {
        var node;
    
        // ...handle this element's `style` or `getComputedStyle`...
    
        // Handle child elements
        for (node = elm.firstChild; node; node = node.nextSibling) {
            if (node.nodeType === 1) { // 1 == Element
                walk(node);
            }
        }
    }
    
    // Kick it off starting with the `body` element
    walk(document.body);
    

    That way you don’t build up large, unnecessary temporary structures. This is probably the most efficient way to walk the entire DOM of a document.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.