How to get height of <div> in px dimension
For those looking for a plain JS solution:
let el = document.querySelector("#myElementId");
// including the element's border
let width = el.offsetWidth;
let height = el.offsetHeight;
// not including the element's border:
let width = el.clientWidth;
let height = el.clientHeight;
Check out this article for more details.
Use height()
:
var result = $("#myDiv").height();
alert(result);
This will give you the unit-less computed height in pixels. "px" will be stripped from the result. I.e. if the height is 400px, the result will be 400, but the result will be in pixels.
If you want to do it without jQuery, you can use plain JavaScript:
var result = document.getElementById("myDiv").offsetHeight;
Although they vary slightly as to how they retrieve a height value, i.e some would calculate the whole element including padding, margin, scrollbar, etc and others would just calculate the element in its raw form.
You can try these ones:
javascript:
var myDiv = document.getElementById("myDiv");
myDiv.clientHeight;
myDiv.scrollHeight;
myDiv.offsetHeight;
or in jquery:
$("#myDiv").height();
$("#myDiv").innerHeight();
$("#myDiv").outerHeight();
Use .height()
like this:
var result = $("#myDiv").height();
There's also .innerHeight()
and .outerHeight()
depending on exactly what you want.
You can test it here, play with the padding/margins/content to see how it changes around.