I'm writing a script that needs to check if certain CSS properties are defined inside the <style>
tag.
<style type="text/css">
#bar {width: 200px;}
</style>
<div id="foo" style="width: 200px;">foo</div>
<div id="bar">bar</div>
// 200px
console.log(document.getElementById("foo").style.width);
// an empty string
console.log(document.getElementById("bar").style.width);
if(property_width_defined_in_style_tag) {
// ...
}
Is this possible?
I'm not trying to get the getComputedStyle(ele).width
btw.
I'm not sure this is what you want, it works closest to your first pseudo code where you had an element instance, anyway hope it helps:
I think you can reuse all of this code for your purpose, or just some piece of it, it's modular on purpose – for instance, once you have all the
cssRules
flatten, or theelementRules
, you can still use afor
loop and check what you need. It uses ES5 functions and matchesSelector so in old browsers won't work without shims. Plus, you could also filter by priority and so on – you could for instance remove all the properties has a lower priority than the inline style ones, etc.You can completely explore in javascript the styleSheets.
Start with the
document.styleSheets
array. The values are the different style elements or CSS files that are used by your document.