Lance Pollard
Lance Pollard

Reputation: 79228

Access CSS styles for pseudo-classes in JQuery?

If I define the up and over states of a button like this:

.button {color:red;}
.button:hover {color:blue;}

How can I get all the hover state styles for an element using JQuery?

Something like $(".button:hover").css('color');...

Upvotes: 5

Views: 325

Answers (1)

Nick Craver
Nick Craver

Reputation: 630379

This isn't possible directly, at least not without hovering. The closest you can get is:

$('.button').mouseenter(function() {
  alert($(this).css('color'));
});

But...this requires hovering over the element, which by your question doesn't sound like the goal. Because of the way hovers are done, there was never any reason to expose this information in javascript, the browser css/rendering engine handles all of it...since this is maybe the second time I've seen this asked in years, it's just one of those rarely-used so never-added things, like every other language/platform has a thousand examples of.

Sorry the answer sucks, but "it doesn't do that" is the answer for everything at some point, but will probably remain that way in this case.

Upvotes: 1

Related Questions