I have as the following style in a css file
#galleryImages{
position:absolute;
top:24px;
left:41px;
width:900px;
moving:false;
}
When I try to access it through Javascript, it returns undefined
The external css is correct, it returns other style variables properly as well as the getElementById
alert("External: " + document.styleSheets[0].cssRules[2].style.moving +
"\nInternal: " + document.getElementById("galleryImages").style.moving);
It gives an alert with:
External: Undefined
Internal: Undefined
Is there a way to access a custom CSS variable through javascript?
Thank you in advance
1 Answer 1
Most (all?) browsers won't load unknown CSS into the DOM, and JavaScript cannot directly access CSS styles directly, only the ones that are loaded into the DOM.
The only way I can think of would be to implement your own CSS parsing JavaScript, but for the mostpart, that would probably be excessive for what you want to do, and a pure JavaScript solution or a class value would better.
e.g. In HTML
<div class="moveable"></div>
Using that example, you could use your JavaScript to get the classname of the element, and if it has the "moveable" class, you know it can move.
EDIT: In @Anurag's posted link to the Mozilla bug, it is mentioned that unknown CSS properties are to be ignored as part of the CSS 2.1 specification.
movingCSS property do?