In JavaScript on my website I have something like this:
console.log(document.getElementById("side_news").style.display);
and I have tried this with a lot of styles and it doesn't return anything, just blank. What am I doing wrong?
In JavaScript on my website I have something like this:
console.log(document.getElementById("side_news").style.display);
and I have tried this with a lot of styles and it doesn't return anything, just blank. What am I doing wrong?
Try using getComputedStyle()
:
var sideNewsDiv = document.getElementById('side_news');
getComputedStyle(sideNewsDiv).getPropertyValue("display");
MDN documentation: Window.getComputedStyle().
Most elements don't show all of their attributes when accessing through object.style
. A div
element has a default display style of block
but accessing it through style
will result an empty value.
A solution is to use getComputedStyle
- or, if not supported by the browser, currentStyle
.
if (window.getComputedStyle)
status = window.getComputedStyle(targetElement, null);
else
status = targetElement.currentStyle;
This will show the element's style with all the css changes.