user1365010
user1365010

Reputation: 3363

Get a non inline css from javascript

I know that some would critic what I want, but I will ask my question.


I know that we can't get the style of an element if their css weren't formated inline. So I want a regex (because I am not very good to make one) that will get all the style tags and will apply a regex for their innerHTML to make an array like this one :

css["myDiv"]["color"] -> "red"

Without using getComputedStyle because of the browser incompatibility.

Upvotes: 1

Views: 119

Answers (1)

Mathias Schwarz
Mathias Schwarz

Reputation: 7197

There is no way you achieve anything remotely close to this using regex. CSS rules are applied using a complicated algorithm and rules may be inherited, prioritized and overridden in various ways. It is no coincidence that it took the major browsers years to come up with a compliant implementation.

You should exercise exactly that implementation instead. The tool you need is JavaScript and the API you need is the DOM. For each node in the DOM there is a style object which contains all the applied styles and which has a format very close to what you are trying to generate. You can access this via getComputedStyle and currentStyle.

Upvotes: 3

Related Questions