Javascript : How to get width and height of a div?
Problem :
You have a <div>
and you want to get the dimension of the <div>
such as height and width. You want to ensure that the <div>
dimension is not being tempered by third-party browser's add-ons.
How to do that?
Solution :
Imagine you have a <div id="box">
with id = box. You can get the dimension with getElementById()
function to get the offsetWidth and offsetHeight properties.
For example :
the HTML :
<div id="box">
fill up something here
the more you fill... the higher
the height will be
<br>
<br>
</div>
and the Javascript :
var element = document.getElementById('box');
var height = element.offsetHeight;
alert(height);
var width = element.offsetWidth;
//alert(width)
See demo at http://codepen.io/anon/pen/KdVXLw
IF the height or width is below certain pixels, you can either prompt alert to your website visitors or redirect them to another page.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+11k Golang : Fix - does not implement sort.Interface (missing Len method)
+13.5k Golang : Query string with space symbol %20 in between
+14.4k Golang : Overwrite previous output with count down timer
+19.4k Golang : Close channel after ticker stopped example
+8.8k Golang : Inject/embed Javascript before sending out to browser example
+18.3k Golang : How to get hour, minute, second from time?
+33.5k Golang : All update packages with go get command
+7.3k Golang : Check to see if *File is a file or directory
+25.6k Golang : missing Mercurial command
+9.3k Mac OSX : Get a process/daemon status information
+21k Golang : Clean up null characters from input data