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 8224865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:13:08+00:00 2026-06-07T15:13:08+00:00

The function window.getComputedStyle is supposed to be able to get the computed style of

  • 0

The function window.getComputedStyle is supposed to be able to get the computed style of pseudo classes like :hover, according to the documentation.

It’s also explained as an answer in another question

But as the last comment says in that question, in fact it doesn’t work at all, it just returns the normal style, not the :hover style. You can see for yourself in this jsfiddle. The alert returns the red color, not the green one.

The documentation on developer.mozilla.org also has an example, but that doesn’t work either – see here.

In this question the answerer states in a comment that it won’t work at all, but without giving an explanation.

Could it be that the stylesheet has to be fully rendered before the function returns the correct value? I’ve tried setting some delays, but nothing seems to work.

I’ve tried the latest Firefox, Chrome and IE browsers. Does anybody know why this function is not working as expected?

  • 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-07T15:13:10+00:00Added an answer on June 7, 2026 at 3:13 pm
    var style = window.getComputedStyle(element[, pseudoElt]);
    

    where pseudoElt is “a string specifying the pseudo-element to match”. A pseudo-element is something like ::before or ::after, those are elements which are generated by CSS styles. :hover, :visited or other similar effects are pseuodo-classes. They won’t create new elements but apply specialized class styles to the element.

    It’s not possible to get the computed style of a pseudo-class, at least not with getComputedStyle. You can however traverse through the CSS rules and try to find a fitting selector, but I won’t recommend you to do this, since some browsers won’t follow the DOM-Level-2-Style rules and you would have to check which rule will affect your specific element:

    /* Style */
    p a:hover{ color:yellow; background:black;}
    p > a:hover{ color:green; background:white;}
    p > a+a:hover{ color:fuchsia; background:blue;}
    
    // JS
    var i,j, sel = /a:hover/;
    for(i = 0; i < document.styleSheets.length; ++i){
        for(j = 0; j < document.styleSheets[i].cssRules.length; ++j){    
            if(sel.test(document.styleSheets[i].cssRules[j].selectorText)){
                console.log(
                    document.styleSheets[i].cssRules[j].selectorText,
                    document.styleSheets[i].cssRules[j].style.cssText
                );
            }
        }
    }
    
    Result:
    p a:hover color: yellow; background: none repeat scroll 0% 0% black; 
    p > a:hover color: green; background: none repeat scroll 0% 0% white;
    p > a + a:hover color: fuchsia; background: none repeat scroll 0% 0% blue;

    See also:

    • MDN: getComputedStyle examples
    • W3C: CSS2: 5.10 Pseudo-elements and pseudo-classes
    • SO: Setting CSS pseudo-class rules from JavaScript
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I mean the reverse of this function: styleObj = window.getComputedStyle(node); So something like this:
I have a function: window.onbeforeunload = function () { $.get(Handler.ashx, mode=saveuser&user= + $(#username).text(), function
When I pass 'this' to an anonymous function like so: MyClass.prototype.trigger = function(){ window.setTimeout(function(){this.onTimeout();},1000);
i have structure like this <table><tr><td></td><td><input type=button name=button></td></tr></table> <script> $('tr').click(function(){window.alert('tr');}) $(':input').click(function(){window.alert('input');}) </script> when i
I'm trying to open a new window like so: $('#wrapper').click(function() { window.setTimeout(function() { //alert('hi');
Here's my code: $('#element').hover(function() { window.setTimeout(function() { alert('hovered for 2 seconds'); }, 2000); },
I have an onbeforeunload event : $().ready(function() { window.onbeforeunload=function() { return haha }; });
I try to write a javascript Self-Executing Anonymous Function window.App = window.App || {}
I have this script loaded on a page: (function() { window.alert('bookmarklet started'); function AjaxSuccess(data,
Here is my spaggeti code $(#table_exams tbody tr).click(function () { window.location.hash=# +$(this).attr(exam_ID); window.location.href=/medilab/prototypes/exams/edit?examId= +

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.