why javascript this.style[property] return an empty string?
The .style
property is for getting styles that were placed directly on the element. It doesn't compute styles from your stylesheets.
You may use getComputedStyle()
instead:
const myElement = document.getElementById('myId');
const style = getComputedStyle(myElement);
console.log(style.height); // "100px"
Why element.style always return empty in JS?
element.style returns the inline style used in html document and since the style is not set directly in html you will get and empty value
What you are looking for is the computedStyle which will returns the style that is applied to the element
console.log(window.getComputedStyle(document.getElementById('test')).display)
#map { display: block; align-content:center; }
<div id="test">test</div>
Javascript shows empty style property for document objects that I have styled using the css
This is because element.style
returns the inline CSS style for the element, and not the computed style based on CSS stylesheets.
To get the computed style, use window.getComputedStyle(element)
.
More information here:
- https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
- why javascript this.style[property] return an empty string?
why element.style.backgroundColor property gives an empty string as a result?
.style
contains only inline styles (set via that property, or the HTML attribute with the same name). It's not affected by style sheets at all. getComputedStyle
gets you the current effective value for that property, regardless of where it came from.
The JavaScript style class return empty string
The .style
object only includes style properties directly set on the DOM element. Properties determined from CSS are not visible that way.
style.backgroundColor is an empty string in JavaScript
Unless you have directly defined the backgroundColor on the element itself, you have to use getComputedStyle()
or currentStyle
to get the value of a style property.
A method that is compatible with multiple browsers would look like this:
function getStyle(el,styleProp)
{
if (el.currentStyle)
return el.currentStyle[styleProp];
return document.defaultView.getComputedStyle(el,null)[styleProp];
}
You can see a working example on jsFiddle.
More information:
- See this page for more information about
getComputedStyle()
. - See this page for more information about
currentStyle
(IE). - See this page for more information about browser compatibility issues.
Why browser is returning empty string on style.height ? How to get actual height of an element?
When you use this.style.height, the height must have been specified on the element first, like this:
<div style="height: 15px;" onclick="alert(this.style.height)">sometext</div>
Otherwise, you should probably use offsetHeight or clientHeight:
<div onclick="alert(this.offsetHeight)">sometext</div>
Why are all of my DOM element properties coming back as empty strings?
You can use getComputedStyle
and getPropertyValue
. element.style
will refer to the style
attribute on the element (inline styles), and your CSS isn't inline.
window.addEventListener("load", function(){ var element = document.getElementsByClassName("js-child")[0]; var rightPosition = window.getComputedStyle(element).getPropertyValue('right'); console.log(rightPosition)});
.parent { position: relative;}
.child { position: absolute; right: -220px; top: 0; transition: all 0.5s ease;}
<div class="parent"> <div class="child js-child"> </div></div>
Javascript style.left is empty string
Why would this be?
Presumably because it hasn't been set to anything.
Isn't style.left supposed to return an integer?
No. It is supposed to return a string containing the value of the CSS left property as set directly on the element (either by setting the JS property itself or by using a style attribute). It does not get a value from the cascade and it should only be an integer if the value is 0 (since all other lengths require units).
See How to get computed style of a HTMLElement if you want to get the computed value for the property rather than what I described in the previous paragraph.
Related Topics
How to Display an Image Stored as Byte Array in Html/JavaScript
How to Use Sockets in JavaScript\Html
Cross-Origin Data in Html5 Canvas
Html Form Action and Onsubmit Issues
What Is Innerhtml on Input Elements
Pass Vars to JavaScript Via the Src Attribute
Ternary Operator in Angularjs Templates
Html "Overlay" Which Allows Clicks to Fall Through to Elements Behind It
Flask - Calling Python Function on Button Onclick Event
How to Draw an Oval in Html5 Canvas
Onclick Open Window and Specific Size
Jquery Ui Accordion That Keeps Multiple Sections Open
Benefits of Loading Js At the Bottom as Opposed to the Top of the Document
Dynamically Creating HTML Elements Using JavaScript
Document.Write() Overwriting the Document
How to Write in File (User Directory) Using JavaScript