How would I check if a style is defined on an element? Or how do I check if the style value is inherited or not?
I have a div that sometimes may be inheriting a style value. I want to know if the style value is inherited.
var element = document.getElementById("a");
var property = "--my-style";
var value = element.style.getPropertyValue(property);
var computedValue = window.getComputedStyle(element).getPropertyValue(property);
console.log("A value:" + value);
console.log("A computed value:" + computedValue);
element = document.getElementById("b");
value = element.style.getPropertyValue(property);
computedValue = window.getComputedStyle(element).getPropertyValue(property);
console.log("B value:" + value);
console.log("B computed value:" + computedValue);
#a {
--my-style: A;
}
#b {
}
<div id="a">
<div id="b">
Hello World
</div>
</div>
Does the code above work?