Get Element StyleSheet Style in JavaScript

Is it possible to get only the
stylesheet specified styles of an
element (and return null if the style
is undefined)?

That’s effectively what is done by the routine you present. The problem is, in most scenarios, most styles are not undefined – they’re inherited and/or defined by the individual browser’s internal stylesheet.

You could, with a whole lot of effort, iterate through all of the rules defining the style in question, in all of the stylesheets in the current view of the document, evaluate them for the element in question, and if none applied… and if none applied to a parent (or this particular style is not inherited)… then consider it undefined. This would be slow, and incredibly error-prone. I would not recommend trying it.

Perhaps you would do better to step back and ask why you would ever need such a thing?

Leave a Comment