How does one get the ultimate, final, after-inheritance style value for an HTML element in JavaScript? I already know that element.style
only gets the inline styles, and that getComputedStyle()
only does elementary transformations on the inline styles (it doesn't appear to be inheriting anything from the style sheets). How do I get the inherited style values?
I'd prefer not to use jQuery (since I know that a pure JavaScript solution is possible, though jQuery is already in use in my application). I don't know what the stylesheet configuration is ahead of time because the sheets are being compiled together with SASS.
My specific case is that I'm trying to get a business-defined color from the stylesheet for a canvas-based drawing task, and the colors are defined in the CSS. I'm currently trying to use code like the following to extract my entities.
var testElement = document.createElement("span");
var entity_type_color_table = {};
function getEntityTypeColor(entity_type) {
if (!entity_type_color_table[entity_type]) {
testElement.className = entity_type + "-color";
entity_type_color_table[entity_type] =
document.defaultView.getComputedStyle(testElement, null)
.getPropertyValue("color");
}
return entity_type_color_table[entity_type];
}