5

On page ready I want to run a function that will get the css background-image setting and also the hover background-image setting which I wish to then utilize in another fashion.

The only method I have seen to accomplish this requires someone to actually mouse over the element before you can get the value.

Anyone have a way?

This is how to get a css non-hover value:

$(this).css('background-color');
1
  • have you tried $("#"+this.id+":HOVER").css("background-color"); ? Commented Mar 15, 2011 at 23:59

1 Answer 1

4

To do this you will need to read values from the stylesheet directly. The basic idea is to iterate through each stylesheet in document.styleSheets, and within these, iterate through each rule in .cssRules or .rules, looking for a .selectorText that matches what you're after.

To simplify things, I wrote a (tiny) library for this purpose - JSS. If you have a selector, for example #someId:hover, then using JSS, you can get its background-color property with:

jss('#someId:hover').get()['background-color']
Sign up to request clarification or add additional context in comments.

1 Comment

jss fails the actually-does-something test, alas: jss("#espacio_publico").get() -> TypeError: Cannot read property 'length' of null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.