There must be something simple I'm missing. I'm using only vanilla Javascript to return DOM element properties. The problem is, I'm only getting empty strings when I try to get any style property from my DOM elements.
Here is a snippet of my HTML:
<div class="parent">
<div class="child js-child">
</div>
</div>
Here is the CSS:
.parent {
position: relative;
}
.child {
position: absolute;
right: -220px;
top: 0;
transition: all 0.5s ease;
}
Some JS code:
window.addEventListener("load", function(){
var element = document.getElementsByClassName("js-child")[0];
var rightPosition = element.style.right;
});
When I put in a debugger and check the value, rightPosition = ""
.
It should be rightPosition = "-220px"
. No matter what property I'm trying to get, it all comes back as ""
(I've tried checking the display, top property, and several others).
I want to run a simple if statement, if (rightPosition === "-220px")
, but it's always coming back as an empty string, so the if statement doesn't check out.
When I look in the chrome inspector, all of the CSS is set properly and working.
Any ideas as to why I'm only getting empty strings?
Thanks!