Alex Rivera | Logout

Get css value without DOM element

Asked 2009-10-17T16:07:05.787
15

I am wondering if there is a way to get css value from stylesheet file when there is no element in the DOM using it? I am using jQuery and I use selector $(".classname").css() to get the values. But with the case "classname" is not in any element, what to do to get the value" Thanks

Edit
Report

1 Answer

5

See http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript which presents a cross-browser approach to getting and adding css dynamically.

It works with document.styleSheets and both IE's .rules and everyone else's .cssRules

It also has the advantage of being somewhat abstracted so you don't need to worry about the details.


The above link no longer works. The following is a screenshot of the blog-article, captured by the internet archive wayback in 2008.

enter image description here

The function basically iterates over all styles of all stylesheets and provides the ability to modify / delete them.
Please note that this cannot be recommended, since most modern stylesheets are too large to make this an efficient operation.

answered 2009-10-17T16:13:16.150

Your Answer