Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');...

like image 665
Lance Avatar asked Nov 14 '22 11:11

Lance


1 Answers

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.

like image 77
Nick Craver Avatar answered Dec 04 '22 08:12

Nick Craver